question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

onNotification() not called when app is inactive (not killed) Android

See original GitHub issue

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" />

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9

github_iconTop GitHub Comments

9reactions
JacobPoldenValtechcommented, Jul 12, 2019

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

1reaction
JacobPoldenValtechcommented, Jul 12, 2019

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

onNotification() is not fired when app is killed - Stack Overflow
I am not positive but when I remember correctly killing (alias force closing) an app disables the onNotification functionality in Android ...
Read more >
NotificationListenerService - Android Developers
The service should wait for the onListenerConnected() event before performing any ... MODE_MULTI_PROCESS does not work reliably in some versions of Android, ...
Read more >
Handling incoming iOS notifications - Pusher Beams Docs
Use Beams to send iOS notifications and trigger callback code in your app when a notification is recieved so that you can respond...
Read more >
awesome_notifications | Flutter Package - Pub.dev
A complete solution to create Local and Push Notifications, customizing buttons, images, sounds, emoticons and applying many different layouts for Flutter ...
Read more >
Notifications Not Shown - Mobile Push
App Push Permissions Disabled - Device Not Subscribed. Android,iOS: In your Device Settings > Notifications > Your App, you can turn on and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found