diff --git a/Examples/simple-fcm-client/app/PushController.js b/Examples/simple-fcm-client/app/PushController.js index e61d905c2e48cb1e80ff4568ce7b197f5a302b01..717ed4bcf7e54d2a49a41b733103dded5d55abab 100644 --- a/Examples/simple-fcm-client/app/PushController.js +++ b/Examples/simple-fcm-client/app/PushController.js @@ -1,6 +1,6 @@ import React, { Component } from "react"; -import FCM from "react-native-fcm"; +import FCM, {FCMEvent} from "react-native-fcm"; import firebaseClient from "./FirebaseClient"; @@ -21,21 +21,42 @@ export default class PushController extends Component { console.log("INITIAL NOTIFICATION", notif) }); - this.notificationUnsubscribe = FCM.on("notification", notif => { + this.notificationListner = FCM.on(FCMEvent.Notification, notif => { console.log("Notification", notif); - if (notif && notif.local) { + if(notif.local_notification){ return; } - this.sendRemote(notif); + if(notif.opened_from_tray){ + return; + } + + if(Platform.OS ==='ios'){ + //optional + //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 the above documentation link. + //This library handles it for you automatically with default behavior (for remote notification, finish with NoData; for WillPresent, finish depend on "show_in_foreground"). However if you want to return different result, follow the following code to override + //notif._notificationType is available for iOS platfrom + switch(notif._notificationType){ + case NotificationType.Remote: + notif.finish(RemoteNotificationResult.NewData) //other types available: RemoteNotificationResult.NewData, RemoteNotificationResult.ResultFailed + break; + case NotificationType.NotificationResponse: + notif.finish(); + break; + case NotificationType.WillPresent: + notif.finish(WillPresentNotificationResult.All) //other types available: WillPresentNotificationResult.None + break; + } + } + this.showLocalNotification(notif); }); - this.refreshUnsubscribe = FCM.on("refreshToken", token => { + this.refreshTokenListener = FCM.on(FCMEvent.RefreshToken, token => { console.log("TOKEN (refreshUnsubscribe)", token); this.props.onChangeToken(token); }); } - sendRemote(notif) { + showLocalNotification(notif) { FCM.presentLocalNotification({ title: notif.title, body: notif.body, @@ -47,8 +68,8 @@ export default class PushController extends Component { } componentWillUnmount() { - this.refreshUnsubscribe(); - this.notificationUnsubscribe(); + this.notificationListner.remove(); + this.refreshTokenListener.remove(); } diff --git a/Examples/simple-fcm-client/ios/SimpleFcmClient/AppDelegate.m b/Examples/simple-fcm-client/ios/SimpleFcmClient/AppDelegate.m index 96b259613097dd3e269b23857cd6cae4ebdfbe26..8a77da2aeae3eafa9a84ff4be2988870c15ab02f 100644 --- a/Examples/simple-fcm-client/ios/SimpleFcmClient/AppDelegate.m +++ b/Examples/simple-fcm-client/ios/SimpleFcmClient/AppDelegate.m @@ -42,28 +42,20 @@ - (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 { - 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]; } -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { - [[NSNotificationCenter defaultCenter] postNotificationName:FCMNotificationReceived object:self userInfo:notification.userInfo]; + [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]; } @end diff --git a/Examples/simple-fcm-client/package.json b/Examples/simple-fcm-client/package.json index dba51d93f4d22feecbc631d2e8f807f6bea85469..95d4745c4246793116a91cdccf656f4367802b12 100644 --- a/Examples/simple-fcm-client/package.json +++ b/Examples/simple-fcm-client/package.json @@ -9,7 +9,7 @@ "dependencies": { "react": "~15.4.0-rc.4", "react-native": "^0.40.0", - "react-native-fcm": "^4.0.0" + "react-native-fcm": "^6.0.2" }, "jest": { "preset": "jest-react-native" diff --git a/Examples/simple-fcm-client/yarn.lock b/Examples/simple-fcm-client/yarn.lock index a3044e975a98f12ca787d1a1620739c16e5e2ea8..b25855b682388c7b982ac259041a1e32ae430163 100644 --- a/Examples/simple-fcm-client/yarn.lock +++ b/Examples/simple-fcm-client/yarn.lock @@ -2543,13 +2543,13 @@ mime-db@~1.23.0: version "1.23.0" resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.23.0.tgz#a31b4070adaea27d732ea333740a64d0ec9a6659" -mime-types@2.1.11, mime-types@~2.1.7: +mime-types@2.1.11, mime-types@~2.1.7, mime-types@~2.1.9: version "2.1.11" resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.11.tgz#c259c471bda808a85d6cd193b430a5fae4473b3c" dependencies: mime-db "~1.23.0" -mime-types@^2.1.12, mime-types@~2.1.11, mime-types@~2.1.13, mime-types@~2.1.6, mime-types@~2.1.9: +mime-types@^2.1.12, mime-types@~2.1.11, mime-types@~2.1.13, mime-types@~2.1.6: version "2.1.13" resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.13.tgz#e07aaa9c6c6b9a7ca3012c69003ad25a39e92a88" dependencies: @@ -2955,9 +2955,9 @@ react-deep-force-update@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/react-deep-force-update/-/react-deep-force-update-1.0.1.tgz#f911b5be1d2a6fe387507dd6e9a767aa2924b4c7" -react-native-fcm@^4.0.0: - version "4.0.0" - resolved "https://registry.yarnpkg.com/react-native-fcm/-/react-native-fcm-4.0.0.tgz#c87bd442cdf05c900c7d436da518b070ece19162" +react-native-fcm@^6.0.2: + version "6.0.2" + resolved "https://registry.yarnpkg.com/react-native-fcm/-/react-native-fcm-6.0.2.tgz#432578deaf9ad0fa968424c1babb5563c68eaa7e" react-native@^0.40.0: version "0.40.0"