diff --git a/README.md b/README.md index ef0d91019b8b9289f530c40b05240cdc1289fc2d..93a2714ca8dcc1298b2ccb6a632ac5fbf736d4a6 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(){