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 is not called when remote notification arrives and app is in background/foreground

See original GitHub issue

I’m to update the UI when onNotification arrives. In general onNotification works as expected when app is killed. It wakes up -> onNotification gets called.

BUT onNotification doesn’t seem to be getting called when the app is NOT killed, i.e. in foreground or background.

I saw people reporting similar issues for local notifs, but I assume for remote notifications it’s different?

Can someone suggest what could be wrong?

import React from 'react'
import { Platform } from 'react-native'
import PushNotification from 'react-native-push-notification'
import PushNotificationIOS from '@react-native-community/push-notification-ios'

const Root = () => {
  PushNotification.configure({
    onRegister: pushToken => {
      console.log(pushToken.token)
    },

    onNotification: notification => {
      console.log('Notification received', notification)
      notification.finish(PushNotificationIOS.FetchResult.NoData)
    },
    onError: err => {
      console.log('Error configuring notifications', err)
    },

    senderID: SENDER_ID,

    permissions: {
      alert: true,
      badge: true,
      sound: true,
    },

    popInitialNotification: true,
    requestPermissions: Platform.OS === 'android',
  })

  return <Router />
}

The library version:

    "react-native-push-notification": "^3.5.0",

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:20
  • Comments:22

github_iconTop GitHub Comments

3reactions
Pra3t0r5commented, May 31, 2021

@alex-mironov @snamstorm I am now facing the opposite problem. onNotification is called correctly when coming from foreground or background but it is not called when the app was killed. The app just launches normally. Could you maybe let me know how you did the configuration? I am calling PushNotification.configure from index.js so timing should not be the issue I guess. For Android it works correctly this way. I will also create a new issue for it since this is not really related. Just wanted your feedback since it seems to be working for you 😉

How did you solve this?

Haven’t solved it yet

This solved it for me:

  • SplashActivity
package com.trique.app; // Change this to your package name.

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class SplashActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtras(getIntent().getExtras());  // <--- ADD THIS
        startActivity(intent);
        finish();
    }
}
  • MainActivity
package com.trique.app;

import com.facebook.react.ReactActivity;
import org.devio.rn.splashscreen.SplashScreen;
import android.os.Bundle;
import android.content.Intent;

public class MainActivity extends ReactActivity {

  /**
   * Returns the name of the main component registered from JavaScript. This is used to schedule
   * rendering of the component.
   */
  @Override
  protected String getMainComponentName() {
    return "com.trique.app";
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      SplashScreen.show(this);
      super.onCreate(savedInstanceState);
  }
}
  • AndroidManifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.trique.app"
  xmlns:tools="http://schemas.android.com/tools">
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.CAMERA"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.RECORD_AUDIO"/>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  <uses-permission android:name="android.permission.VIBRATE"/>
  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
  <application android:name=".MainApplication" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:allowBackup="false" android:theme="@style/AppTheme">
    <meta-data android:name="com.dieam.reactnativepushnotification.notification_foreground" android:value="false"/>
    <meta-data android:name="com.dieam.reactnativepushnotification.notification_color" android:resource="@color/white"/>
    <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationActions"/>
    <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"/>
        <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
        <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
      </intent-filter>
    </receiver>

    <service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService" android:exported="false">
      <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
      </intent-filter>
    </service>

    <activity android:name=".SplashActivity" android:theme="@style/SplashTheme" android:launchMode="singleTask" android:label="@string/app_name">

      <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
      </intent-filter>
    </activity>

    <activity android:name=".MainActivity" android:label="@string/app_name" android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode" android:windowSoftInputMode="adjustResize" android:launchMode="singleTask" android:exported="true">
      <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:host="share.triqueapp.com" android:scheme="https"/>
      </intent-filter>
    </activity>

    <activity android:name="com.facebook.react.devsupport.DevSettingsActivity"/>

  </application>
</manifest>
2reactions
jSkrillscommented, Jun 5, 2020

@Dallas62 you were right. On the push-notification-ios#107 issue you eluded to the fix pointed out here enabled me to begin receiving onNotification events while the app was in the background or foreground. Thanks for your pointers!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android onNotification never called in react-native-push ...
notification received form the server when i clicked on it app is opened but onNotification never called. i put this in componentDidMount() ...
Read more >
navigating user on notification click app in background ...
Coding example for the question navigating user on notification click app in background/foreground-React Native.
Read more >
didReceiveRemoteNotification not c… - Apple Developer
From the docs it states that this method is called even when the app is in background: Use this method to process incoming...
Read more >
Handling incoming iOS notifications - Pusher Beams Docs
When a notification arrives whilst your app is running (foreground or ... method will not be called until the user brings the app...
Read more >
How to perform action when user tap on push notification ...
To handle push notification in App Delegate, we will set the ... When the app launch after user tap on notification (originally was...
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