Created
October 14, 2020 04:28
-
-
Save BunHouth/80d587e15913b0fd962257f5b7b95180 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import messaging from '@react-native-firebase/messaging'; | |
import PushNotification from 'react-native-push-notification'; | |
import PushNotificationIOS from '@react-native-community/push-notification-ios'; | |
import { navigate } from '@navigator/navigator'; | |
import { registerDeviceToken } from '@state/auth/ActionCreator'; | |
export const notificationHandler = data => { | |
if (data?.notify_type === 'comment') { | |
navigate('PostDetail', { | |
postId: data.target_id, | |
trigger: 'comment', | |
}); | |
} else { | |
if (['like'].includes(data?.notify_type)) { | |
navigate('PostDetail', { | |
postId: data.target_id, | |
}); | |
} | |
} | |
if (['profile'].includes(data?.notify_type)) { | |
navigate('MyPost', { | |
userId: data.target_id, | |
}); | |
} | |
}; | |
PushNotification.configure({ | |
onNotification: function(notification) { | |
notificationHandler(notification?.data); | |
notification.finish(PushNotificationIOS.FetchResult.NoData); | |
}, | |
permissions: { | |
alert: true, | |
badge: true, | |
sound: true, | |
}, | |
popInitialNotification: false, | |
requestPermissions: false, | |
}); | |
const localNotification = remoteMessage => { | |
const { notification, messageId, data } = remoteMessage; | |
let userInfo = { ...data }; | |
if (!userInfo.id) { | |
userInfo.id = messageId; | |
} | |
PushNotification.localNotification({ | |
bigText: notification.body || '', | |
subText: notification.title, | |
priority: 'high', | |
importance: 'high', | |
messageId: messageId, | |
invokeApp: true, | |
title: notification.title, | |
message: notification.body || '', | |
userInfo: userInfo, | |
soundName: 'default', | |
}); | |
}; | |
const initFirebase = navigation => { | |
messaging().onMessage(localNotification); | |
messaging().onNotificationOpenedApp(notificationOpen => { | |
const { data } = notificationOpen; | |
notificationHandler(data); | |
}); | |
}; | |
export const checkFirebasePermission = async (navigation, dispatch) => { | |
await messaging().registerDeviceForRemoteMessages(); | |
const authStatus = await messaging().requestPermission(); | |
const enabled = | |
authStatus === messaging.AuthorizationStatus.AUTHORIZED || | |
authStatus === messaging.AuthorizationStatus.PROVISIONAL; | |
if (enabled) { | |
dispatch(registerDeviceToken()); | |
} | |
initFirebase(navigation); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment