From e1b1b418468394f68f65f662245a41f9a8f4da62 Mon Sep 17 00:00:00 2001 From: Libin Lu Date: Tue, 31 Jan 2017 09:30:35 -0500 Subject: [PATCH] update example for v6 --- .../simple-fcm-client/app/PushController.js | 37 +++++++++++++++---- .../ios/SimpleFcmClient/AppDelegate.m | 16 ++------ Examples/simple-fcm-client/package.json | 2 +- Examples/simple-fcm-client/yarn.lock | 10 ++--- 4 files changed, 39 insertions(+), 26 deletions(-) diff --git a/Examples/simple-fcm-client/app/PushController.js b/Examples/simple-fcm-client/app/PushController.js index e61d905..717ed4b 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 96b2596..8a77da2 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 dba51d9..95d4745 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 a3044e9..b25855b 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" -- 2.26.2