Saturday, September 1, 2018

onNotification() is not fired when app is killed

Leave a Comment

i'm using https://github.com/zo0r/react-native-push-notification to get push notifications and the onNotification is working as aspected when the app is Active or in the Background i'm receiving the notification, but when i kill the app i only get the notification and the onNotification is not fired can any one please help i searched for a solution but nothing worked, i'm increasing the Android badges count when the onNotification is fired is there another way to increasing Android badges when the app is killed?

"react-native": "0.55.3", "react-native-push-notification": "3.0.2" 

my code

const options = {         // (optional) Called when Token is generated (iOS and Android)         onRegister: (token) => {             this._TOKEN = token.token;             this._addToPushChannels(channel);         },          onNotification: (notification) => {             console.log(notification);             if (notification['foreground']) {              }              //Notification from the background or when the app is killed             if (!notification['foreground']) {                 if (Platform.OS !== 'ios' && Platform.Version < 26) {                     this._messageCount++;                     // console.log('this._messageCount ', this._messageCount);                     let BadgeAndroid = require('react-native-android-badge');                     BadgeAndroid.setBadge(this._messageCount);                 }             }              // required on iOS only (see fetchCompletionHandler docs: https://facebook.github.io/react-native/docs/pushnotificationios.html)             notification.finish(PushNotificationIOS.FetchResult.NewData);          },          // ANDROID ONLY: GCM Sender ID (optional - not required for local notifications, but is need to receive remote push notifications)         senderID: this._SENDER_ID,          // IOS ONLY (optional): default: all - Permissions to register.         permissions: {             alert: true,             badge: true,             sound: true         },          // Should the initial notification be popped automatically         // default: true         popInitialNotification: true,          /**          * (optional) default: true          * - Specified if permissions (ios) and token (android and ios) will requested or not,          * - if not, you must call PushNotificationsHandler.requestPermissions() later          */         requestPermissions: true,      };      PushNotification.configure(options); 

2 Answers

Answers 1

I am not positive but when I remember correctly killing (alias force closing) an app disables the onNotification functionality in Android (GCM) Push Notification Gateway (unlike iOS/APNS). This is a Android-wise feature and intentionally created so that users who willingly force close their app dont get "bothered" with notifications. The Xtify service will be restarted and messages will be received when either the phone is restarted or the app is reopened.

What you could do is schedule notifications apriori. WhatsApp, for instances, frequently posts notifications like: "You may have new notifications" when the app is forced closed to remind the user to reopen the app.

References:

Answers 2

[1] https://stackoverflow.com/a/37845174/3607191

[2] https://stackoverflow.com/a/47857531/3607191

[3] https://github.com/dluksza/react-native-push-notification/commit/5bb95881c27b0068249c0f68bbf65e54a565fa3d#diff-54ebb78785872f03fd8e71ed2dfed620R18

You can invoke headless function on JS side to update the batch count, following is what you have to do. Here, the catch is you've to write some Java code.

  1. As explained in [2] you have to write a service extending FirebaseMessagingService.
  2. Write one more service by extending HeadlessJsTaskService to invoke headless JS function. Refer RNPushNotificationActionService.java file of [3].
  3. Now, as explained in [1] within onMessageReceived function of the service, call the native service you wrote in step 2 to invoke headless JS function. To know how to do, refer RNPushNotificationActionHandlerReceiver.java file of [3].
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment