React-native-push-notification: onNotification() not called when app is inactive (not killed) Android

Created on 5 Jul 2019  路  9Comments  路  Source: zo0r/react-native-push-notification

I am using FCM as my remote notification server. Sending notifications hits the onNotification() callback in these two scenarios:

  • the app is in the foreground, ie: I am using the app and its taking the whole of my screen
  • the app is "killed", ie: I have swiped up and the app is no longer available in recent apps.

The onNotification() is not hit in this scenario:

  • the app is running, but I have switched to WhatsApp (or any app). The notification is received. Upon tapping the notification the app is brought into focus but the onNotification() callback is not hit.

It appears that the app restarts rather than just bringing the app into focus. The app performs as expected when switching between other apps, it's only interacting with the notification that appears to trigger this restart.

This issue sounds related to #972 but the solution suggested does not work for me.

package.json

"dependencies": {
    "base-64": "^0.1.0",
    "email-validator": "^2.0.4",
    "firebase": "^5.10.1",
    "firebase-admin": "~5.12.1",
    "firebase-functions": "^1.0.3",
    "he": "^1.1.1",
    "isomorphic-fetch": "^2.2.1",
    "lodash": "^4.17.10",
    "lottie-react-native": "^2.5.9",
    "moment": "^2.22.2",
    "npm": "^6.5.0",
    "prop-types": "^15.6.1",
    "react": "^16.7.0",
    "react-native": "^0.57.8",
    "react-native-app-auth": "4.0.0",
    "react-native-background-timer": "^2.1.0-alpha.6",
    "react-native-config": "^0.11.5",
    "react-native-custom-tabs": "^0.1.7",
    "react-native-google-analytics-bridge": "^5.9.0",
    "react-native-push-notification": "^3.1.3",
    "react-native-safari-view": "^2.1.0",
    "react-native-svg": "^8.0.8",
    "react-native-tab-view": "^1.2.0",
    "react-navigation": "^2.0.4",
    "rn-fetch-blob": "^0.10.15"
  },
  "devDependencies": {
    "kleur": "^3.0.1",
    "flatted": "^2.0.0",
    "socks": "^2.2.2",
    "ajv-keywords": "^3.2.0",
    "node-pre-gyp": "^0.12.0",
    "ansi-colors": "^3.2.3",
    "@babel/core": "^7.2.2",
    "@babel/plugin-proposal-class-properties": "^7.2.3",
    "babel-core": "^7.0.0-bridge.0",
    "babel-eslint": "^8.2.3",
    "babel-jest": "^23.6.0",
    "babel-preset-react-native": "^5.0.2",
    "eslint": "^4.19.1",
    "eslint-config-airbnb": "^16.1.0",
    "eslint-config-prettier": "^2.9.0",
    "eslint-plugin-import": "^2.12.0",
    "eslint-plugin-jest": "^21.25.1",
    "eslint-plugin-jsx-a11y": "^6.0.3",
    "eslint-plugin-prettier": "^2.6.0",
    "eslint-plugin-react": "^7.8.2",
    "firebase-functions-test": "^0.1.1",
    "jest": "^23.6.0",
    "metro-react-native-babel-preset": "^0.51.1",
    "prettier": "^1.15.3",
    "react-test-renderer": "^16.8.0",
    "regenerator-runtime": "^0.12.1"
  },

Configure method

PushNotification.configure({
        onRegister,
        onNotification,
        onError,
        senderID: appConfig.fcmSenderId,
        // IOS ONLY (optional): default: all - Permissions to register.
        permissions: {
            alert: true,
            badge: true,
            sound: 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,
    });

Android Manifest snippet

<!--Push notifcations-->
        <meta-data android:name="com.dieam.reactnativepushnotification.notification_channel_name" android:value="News"/>
        <meta-data android:name="com.dieam.reactnativepushnotification.notification_channel_description" android:value="Keep up to date with News"/>
        <!-- Change the resource name to your App's accent color - or any other color you want -->
        <meta-data android:name="com.dieam.reactnativepushnotification.notification_color" android:resource="@color/red"/>
        <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
        <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
        <service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationRegistrationService"/>
        <service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService" android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
        <meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/ic_notification" />
        <meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@color/red" />

Most helpful comment

Closing issues - switched to using https://rnfirebase.io/, would suggest if you are reading this, you do too.

All 9 comments

+1

We will soon release. Please, fix this bug.

hi @JacobPoldenValtech,
did you find any solution for this.
We are facing the same issue.

https://github.com/zo0r/react-native-push-notification/issues/727#issuecomment-386360518 this works for me (on iOS it fires notification handler twice, once at onNotification and once at popInitialNotification)

Hi @adimshev , Thanks for the reply.
But it is not working in my case.
When going from foreground to background and tapping on notification from the tray is not invoking onNotification Handler.
Tried many solutions nothing Helped.
It was working earlier but after google breaking change of android X. We chagned our gradle file to point the specific version instead of ":+"
This is how our build.gradle file looks like this after changing to fix version:

{
googlePlayServicesVersion = "17.0.0"//"16.1.0" // default: "+"
buildToolsVersion = "27.0.3"
minSdkVersion = 16
targetSdkVersion = 26
supportLibVersion = "26.1.0"
compileSdkVersion = 29
firebaseVersion = "17.3.4"
}

Hi @adimshev , Thanks for the reply.
But it is not working in my case.
When going from foreground to background and tapping on notification from the tray is not invoking onNotification Handler.
Tried many solutions nothing Helped.
It was working earlier but after google breaking change of android X. We chagned our gradle file to point the specific version instead of ":+"
This is how our build.gradle file looks like this after changing to fix version:

{
googlePlayServicesVersion = "17.0.0"//"16.1.0" // default: "+"
buildToolsVersion = "27.0.3"
minSdkVersion = 16
targetSdkVersion = 26
supportLibVersion = "26.1.0"
compileSdkVersion = 29
firebaseVersion = "17.3.4"
}

onNotification not invoking, so you can use popInitialNotification https://github.com/zo0r/react-native-push-notification/issues/727#issuecomment-386360518

@kashifaliquazi I ended up dropping this package in the end. Moved to React-Native-Firebase and everything worked straight away. Documentation and features are miles ahead. Would recommend you do the same if you can.

Closing issues - switched to using https://rnfirebase.io/, would suggest if you are reading this, you do too.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ssolida picture ssolida  路  3Comments

cidevant picture cidevant  路  3Comments

selimonline picture selimonline  路  3Comments

uendar picture uendar  路  3Comments

edwinharly picture edwinharly  路  3Comments