Skip to content

Instantly share code, notes, and snippets.

@BunHouth
Created October 14, 2020 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BunHouth/80d587e15913b0fd962257f5b7b95180 to your computer and use it in GitHub Desktop.
Save BunHouth/80d587e15913b0fd962257f5b7b95180 to your computer and use it in GitHub Desktop.
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