Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react-native-fcm 10.0.3 upgrade #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 53 additions & 16 deletions shoutem.firebase/app/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import FCM from 'react-native-fcm';
import { Platform } from 'react-native';
import FCM, { FCMEvent } from 'react-native-fcm';

import { isProduction } from 'shoutem.application';

Expand All @@ -16,11 +17,33 @@ function appDidMount(app) {
const store = app.getStore();

const { dispatch } = store;

const dispatchNotificationAction = (receivedNotification) => {
const { body, title, action, opened_from_tray } = receivedNotification;
const notification = { body, openedFromTray: opened_from_tray, title };


const dispatchNotificationAction = (receivedNotification) => {
if (receivedNotification.fcm) {
var { fcm, action, opened_from_tray, show_in_foreground } = receivedNotification;
var notification = { body: fcm.body, openedFromTray: opened_from_tray, title: fcm.title };

if (show_in_foreground) {
FCM.presentLocalNotification({
title: notification.title,
body: notification.body,
sound: "default",
priority: "high",
click_action: action,
ticker: notification.body,
auto_cancel: true,
vibrate: 300,
ongoing: false,
show_in_foreground: true
});
}
} else if (receivedNotification.aps) {
var { aps, action, opened_from_tray } = receivedNotification;
var notification = { body: aps.alert, openedFromTray: opened_from_tray, title: receivedNotification['google.c.a.c_l'] };
} else {
return;
}

if (action) {
try {
const actionObject = JSON.parse(action);
Expand All @@ -33,21 +56,35 @@ function appDidMount(app) {
dispatch(notificationReceived(notification));
};

FCM.getFCMToken().then((token) => {
handleReceivedToken(token, dispatch);
// this shall be called regardless of app state: running, background or not running. Won't be called when app is killed by user in iOS
FCM.on(FCMEvent.Notification, async (notif) => {
if (notif) {
dispatchNotificationAction({ ...notif });

// iOS requires developers to call completionHandler to end notification process. If you do not call it your background remote notifications could be throttled, to read more about it see https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623013-application.
if (Platform.OS === 'ios') {
notif.finish();
}
}
});

FCM.on('notification', dispatchNotificationAction);

FCM.getInitialNotification().then((notification) => {
if (notification) {
dispatchNotificationAction({ ...notification, opened_from_tray: true });
}

FCM.on(FCMEvent.RefreshToken, (token) => {
// fcm token may not be available on first load, catch it here
handleReceivedToken(token, dispatch);
});

FCM.on('refreshToken', (token) => {
FCM.getFCMToken().then((token) => {
handleReceivedToken(token, dispatch);
});

// initial notification contains the notification that launchs the app. If user launchs app by clicking banner, the banner notification info will be here rather than through FCM.on event
// sometimes Android kills activity when app goes to background, and when resume it broadcasts notification before JS is run. You can use FCM.getInitialNotification() to capture those missed events.
// initial notification will be triggered all the time even when open app by icon so send some action identifier when you send notification
FCM.getInitialNotification().then(notif => {
if (notif) {
dispatchNotificationAction({ ...notif });
}
});
}

export {
Expand Down
7 changes: 0 additions & 7 deletions shoutem.firebase/app/firebase.android.js

This file was deleted.

2 changes: 0 additions & 2 deletions shoutem.firebase/app/firebase.ios.js

This file was deleted.

2 changes: 1 addition & 1 deletion shoutem.firebase/app/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from 'shoutem.push-notifications';
import { isProduction } from 'shoutem.application';

import FCM from './firebase';
import FCM from 'react-native-fcm';

// eslint-disable-next-line no-unused-vars
const requestPermissions = store => next => action => {
Expand Down
2 changes: 1 addition & 1 deletion shoutem.firebase/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Firebase SDK bridge",
"dependencies": {
"request": "2.79.0",
"react-native-fcm": "5.0.0",
"react-native-fcm": "10.0.3",
"xcode": "0.8.9",
"lodash": "^4.17.2",
"urijs": "1.18.5"
Expand Down
15 changes: 8 additions & 7 deletions shoutem.firebase/app/scripts/android/copy_custom_manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ permissions.forEach(permission => {

// add firebase services
const services = `
<service android:name="com.evollu.react.fcm.MessagingService">
<service android:name="com.evollu.react.fcm.MessagingService" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
Expand All @@ -36,14 +36,15 @@ const services = `
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>

<receiver android:name="com.evollu.react.fcm.FIRLocalMessagingPublisher"/>
<receiver android:enabled="true" android:exported="true" android:name="com.evollu.react.fcm.FIRSystemBootEventReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
`;

Expand Down
32 changes: 12 additions & 20 deletions shoutem.firebase/app/scripts/ios/add_native_code_mods.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ if (~appDelegateContents.indexOf(firHeaderImportStatement)) {
// 4. Add to applicationDidFinishLaunchingWithOptions
const notificationCenterInitializationPatch = `
[FIRApp configure];
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
#endif
`;

if (~appDelegateContents.indexOf(notificationCenterInitializationPatch)) {
Expand All @@ -60,34 +58,29 @@ if (~appDelegateContents.indexOf(notificationCenterInitializationPatch)) {

// 5. Add methods to app delegate body
const addMethodsPatch = `
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
[[NSNotificationCenter defaultCenter] postNotificationName:FCMNotificationReceived object:self userInfo:notification.request.content.userInfo];
if([[notification.request.content.userInfo valueForKey:@"show_in_foreground"] isEqual:@YES]){
completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound);
}else{
completionHandler(UNNotificationPresentationOptionNone);
}

[RNFIRMessaging willPresentNotification:notification withCompletionHandler:completionHandler];
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
#if defined(__IPHONE_11_0)
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
{
NSDictionary* userInfo = [[NSMutableDictionary alloc] initWithDictionary: response.notification.request.content.userInfo];
[userInfo setValue:@YES forKey:@"opened_from_tray"];
[[NSNotificationCenter defaultCenter] postNotificationName:FCMNotificationReceived object:self userInfo:userInfo];
[RNFIRMessaging didReceiveNotificationResponse:response withCompletionHandler:completionHandler];
}
#else
//You can skip this method if you don't want to use local notification
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
[[NSNotificationCenter defaultCenter] postNotificationName:FCMNotificationReceived object:self + userInfo:notification.userInfo];
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler
{
[RNFIRMessaging didReceiveNotificationResponse:response withCompletionHandler:completionHandler];
}
#endif

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
[RNFIRMessaging didReceiveLocalNotification:notification];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{
[[NSNotificationCenter defaultCenter] postNotificationName:FCMNotificationReceived object:self userInfo:userInfo];
completionHandler(UIBackgroundFetchResultNoData);
[RNFIRMessaging didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}
`;

Expand All @@ -101,4 +94,3 @@ if (~appDelegateContents.indexOf(addMethodsPatch)) {

fs.writeFileSync(appDelegateHeaderPath, appDelegateHeaderContents);
fs.writeFileSync(appDelegatePath, appDelegateContents);