From 421f1aa03e8fb63f056f1688ee12060f1aec6b7d Mon Sep 17 00:00:00 2001 From: Libin Lu Date: Sat, 9 Sep 2017 12:07:45 -0400 Subject: [PATCH] Update README.md --- README.md | 67 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index ef0d910..93a2714 100644 --- a/README.md +++ b/README.md @@ -284,6 +284,40 @@ NOTE: `com.evollu.react.fcm.FIRLocalMessagingPublisher` is required for presenti import {Platform} from 'react-native'; import FCM, {FCMEvent, RemoteNotificationResult, WillPresentNotificationResult, NotificationType} from 'react-native-fcm'; +// 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) => { + // there are two parts of notif. notif.notification contains the notification payload, notif.data contains data payload + if(notif.local_notification){ + //this is a local notification + } + if(notif.opened_from_tray){ + //app is open/resumed because user clicked banner + } + await someAsyncCall(); + + 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; + } + } +}); +FCM.on(FCMEvent.RefreshToken, (token) => { + console.log(token) + // fcm token may not be available on first load, catch it here +}); + class App extends Component { componentDidMount() { FCM.requestPermissions(); // for iOS @@ -292,45 +326,14 @@ class App extends Component { // store fcm token in your server }); - // NOTE: if you want to handle notifications when app is closed, put another handler outside of component lifecycle because components won't be initialized. this.notificationListener = FCM.on(FCMEvent.Notification, async (notif) => { - // there are two parts of notif. notif.notification contains the notification payload, notif.data contains data payload - if(notif.local_notification){ - //this is a local notification - } - if(notif.opened_from_tray){ - //app is open/resumed because user clicked banner - } - await someAsyncCall(); - - 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.refreshTokenListener = FCM.on(FCMEvent.RefreshToken, (token) => { - console.log(token) - // fcm token may not be available on first load, catch it here + // do some component related stuff }); } componentWillUnmount() { // stop listening for events this.notificationListener.remove(); - this.refreshTokenListener.remove(); } otherMethods(){ -- 2.26.2