react native - not receiving notification when app is killed/not started

4

I am using react-native-firebase@4.2.0 for push notifications.

When app is open on screen, I receive the notification via onNotification and an alert as intended by my code.

PROBLEM 1 When app is in background (not killed), I receive the notification in status bar but on tapping it, it just gets dismissed and app is not opened.

I also get this error in android logcat:

06-21 17:26:03.892  5926  8672 D RNFMessagingService: onMessageReceived event received
06-21 17:26:03.913  5926  8600 E ReactNativeJS: No task registered for key RNFirebaseBackgroundMessage
--------- beginning of crash
06-21 17:26:03.932  5926  8601 E AndroidRuntime: FATAL EXCEPTION: mqt_native_modules
06-21 17:26:03.932  5926  8601 E AndroidRuntime: Process: com.mycustomapp, PID: 5926
06-21 17:26:03.932  5926  8601 E AndroidRuntime: com.facebook.react.common.JavascriptException: No task registered for key RNFirebaseBackgroundMessage, stack:
06-21 17:26:03.932  5926  8601 E AndroidRuntime: startHeadlessTask@57242:24
06-21 17:26:03.932  5926  8601 E AndroidRuntime: __callFunction@4106:49
06-21 17:26:03.932  5926  8601 E AndroidRuntime: <unknown>@3876:31
06-21 17:26:03.932  5926  8601 E AndroidRuntime: __guardSafe@4068:13
06-21 17:26:03.932  5926  8601 E AndroidRuntime: callFunctionReturnFlushedQueue@3875:21
06-21 17:26:03.932  5926  8601 E AndroidRuntime: 
06-21 17:26:03.932  5926  8601 E AndroidRuntime:        at com.facebook.react.modules.core.ExceptionsManagerModule.showOrThrowError(ExceptionsManagerModule.java:56)
06-21 17:26:03.932  5926  8601 E AndroidRuntime:        at com.facebook.react.modules.core.ExceptionsManagerModule.reportFatalException(ExceptionsManagerModule.java:40)
06-21 17:26:03.932  5926  8601 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
06-21 17:26:03.932  5926  8601 E AndroidRuntime:        at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:374)
06-21 17:26:03.932  5926  8601 E AndroidRuntime:        at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:162)
06-21 17:26:03.932  5926  8601 E AndroidRuntime:        at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
06-21 17:26:03.932  5926  8601 E AndroidRuntime:        at android.os.Handler.handleCallback(Handler.java:790)
06-21 17:26:03.932  5926  8601 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:99)
06-21 17:26:03.932  5926  8601 E AndroidRuntime:        at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31)
06-21 17:26:03.932  5926  8601 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:164)
06-21 17:26:03.932  5926  8601 E AndroidRuntime:        at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:194)
06-21 17:26:03.932  5926  8601 E AndroidRuntime:        at java.lang.Thread.run(Thread.java:764)
06-21 17:26:03.938   590   590 E SELinux : avc:  denied  { find } for service=opdiagnose pid=5926 uid=10238 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:opdiagnose_service:s0 tclass=service_manager permissive=0
06-21 17:26:03.939   590   590 E SELinux : avc:  denied  { find } for service=opdiagnose pid=5926 uid=10238 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:opdiagnose_service:s0 tclass=service_manager permissive=0
06-21 17:26:03.939  5926  8601 W OPDiagnose: getService:OPDiagnoseService NULL
06-21 17:26:03.946  5926  8672 W FirebaseMessaging: Error while parsing timestamp in GCM event
06-21 17:26:03.946  5926  8672 W FirebaseMessaging: java.lang.NumberFormatException: s == null
06-21 17:26:03.946  5926  8672 W FirebaseMessaging:     at java.lang.Integer.parseInt(Integer.java:570)
06-21 17:26:03.946  5926  8672 W FirebaseMessaging:     at java.lang.Integer.parseInt(Integer.java:643)
06-21 17:26:03.946  5926  8672 W FirebaseMessaging:     at com.google.firebase.messaging.zzb.zzb(Unknown Source:81)
06-21 17:26:03.946  5926  8672 W FirebaseMessaging:     at com.google.firebase.messaging.zzb.zzc(Unknown Source:2)
06-21 17:26:03.946  5926  8672 W FirebaseMessaging:     at com.google.firebase.messaging.FirebaseMessagingService.zzd(Unknown Source:353)
06-21 17:26:03.946  5926  8672 W FirebaseMessaging:     at com.google.firebase.iid.zzg.run(Unknown Source:26)
06-21 17:26:03.946  5926  8672 W FirebaseMessaging:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
06-21 17:26:03.946  5926  8672 W FirebaseMessaging:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
06-21 17:26:03.946  5926  8672 W FirebaseMessaging:     at com.google.android.gms.common.util.concurrent.zza.run(Unknown Source:7)
06-21 17:26:03.946  5926  8672 W FirebaseMessaging:     at java.lang.Thread.run(Thread.java:764)

PROBLEM 2 When app is killed or not started, I receive this in my android logcat:

W GCM : broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000000 pkg=com.mycustomapp (has extras) }

and that's it. No push notification in status bar or anything.

These are the services added in my AndroidManifest.xml:

<service android:name="io.invertase.firebase.messaging.RNFirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>
<service android:name="io.invertase.firebase.messaging.RNFirebaseInstanceIdService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
</service>

<service android:name="io.invertase.firebase.messaging.RNFirebaseBackgroundMessagingService" />

functions invoked when the app starts:

firebase.messaging().hasPermission()
  .then(enabled => {
      if (enabled) {
          // permissions granted
      } else {
          // permissions not granted
      }
  });

firebase.messaging().getToken()
  .then(fcmToken => {
      if (fcmToken) {
          // user has a device token
          console.log(fcmToken);
      } else {
          // user doesn't have a device token yet
      }
  });

firebase.notifications().getInitialNotification()
  .then((notificationOpen: NotificationOpen) => {
      if (notificationOpen) {
          // App was opened by a notification
          // Get the action triggered by the notification being opened
          // Get information about the notification that was opened
      }
  });

this.onTokenRefreshListener = firebase.messaging().onTokenRefresh(fcmToken => {
  // Process your token as required
  console.log(fcmToken);
});

this.notificationDisplayedListener = firebase.notifications().onNotificationDisplayed((notification: Notification) => {
  // Process your notification as required
  // ANDROID: Remote notifications do not contain the channel ID. You will have to specify this manually if you'd like to re-display the notification.
  console.log("onNotificationDisplayed", notification);
});

this.notificationListener = firebase.notifications().onNotification((notification: Notification) => {
  // Process your notification as required
  Alert.alert(
      notification.title,
      notification.body,
      [
          {text: 'OK', onPress: () => console.log('OK Pressed')},
      ],
      { cancelable: true }
  )
});

this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen: NotificationOpen) => {
  // Get the action triggered by the notification being opened
  const action = notificationOpen.action;
  // Get information about the notification that was opened
  const notification: Notification = notificationOpen.notification;
  console.log("onNotificationOpened", notification);
});

this.messageListener = firebase.messaging().onMessage((message: RemoteMessage) => {
  // Process your message as required
  console.log("onMessage", message);
});

And this is my bgMessaging.js:

import { AppRegistry } from 'react-native';
import firebase from 'react-native-firebase';
// Optional flow type
import type { RemoteMessage } from 'react-native-firebase';

export default async (message: RemoteMessage) => {
    // handle your message
    console.log("RemoteMessage", message);
    return Promise.resolve();
}
AppRegistry.registerHeadlessTask('AppBackgroundMessage', () => bgMessaging);

If it is relevant at all, I use react-native-navigation. If you need any other information, please comment.

android
react-native
react-native-firebase
asked on Stack Overflow Jun 21, 2018 by Ketan Malhotra

3 Answers

2

The headless task must be registered as RNFirebaseBackgroundMessage.

Change your code:

AppRegistry.registerHeadlessTask('AppBackgroundMessage', () => bgMessaging);

To

AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => bgMessaging); 

I hope you have already completed this 4 steps to list for FCM messages in the background.

answered on Stack Overflow Jun 21, 2018 by Aung Myat Hein
0

To Show the notifications in the background also when the App listens to the notification from the firebase you have to create the local notification to display.

I will suggest to use the react-native-fcm library it is very easy to use https://www.npmjs.com/package/react-native-fcm

answered on Stack Overflow Jun 21, 2018 by Murthy Satya
0

Following article can give some reasons if everything is configured correctly.

Why your Push Notifications never see the light of day

answered on Stack Overflow Sep 8, 2020 by Yohan Karunanayake

User contributions licensed under CC BY-SA 3.0