简体   繁体   English

本机反应-应用程序被杀死/未启动时未收到通知

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

I am using react-native-firebase@4.2.0 for push notifications.我正在使用react-native-firebase@4.2.0进行推送通知。

When app is open on screen, I receive the notification via onNotification and an alert as intended by my code.当应用程序在屏幕上打开时,我会通过onNotification收到通知,并收到我的代码预期的警报。

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.问题 1当应用程序在后台(未被杀死)时,我在状态栏中收到通知,但在点击它时,它只是被关闭并且应用程序未打开。

I also get this error in android logcat:我也在 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:问题 2当应用程序被终止或未启动时,我在 android logcat 中收到此消息:

W GCM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000000 pkg=com.mycustomapp (has extras) } W GCM:广播意图回调:result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000000 pkg=com.mycustomapp(有附加功能)}

and that's it.就是这样。 No push notification in status bar or anything.状态栏或任何内容中都没有推送通知。

These are the services added in my AndroidManifest.xml :这些是在我的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 :这是我的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 .如果完全相关,我会使用react-native-navigation If you need any other information, please comment.如果您需要任何其他信息,请发表评论。

The headless task must be registered as RNFirebaseBackgroundMessage.无头任务必须注册为 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.我希望您已经完成了这 4 个步骤来在后台列出 FCM 消息。

Background & Kill state messages背景 & 杀死 state 条消息

When the application is in a background or Kill state, the onMessage handler will not be called when receiving messages.当应用程序处于后台或 Kill state 时,接收消息时不会调用 onMessage 处理程序。 Instead, you need to setup a background callback handler via the setBackgroundMessageHandler method.相反,您需要通过 setBackgroundMessageHandler 方法设置后台回调处理程序。

To setup a background handler, call the setBackgroundMessageHandler outside of your application logic as early as possible:要设置后台处理程序,请尽早在应用程序逻辑之外调用 setBackgroundMessageHandler:

// index.js import { AppRegistry } from 'react-native'; // index.js import { AppRegistry } from 'react-native'; import messaging from '@react-native-firebase/messaging';从“@react-native-firebase/messaging”导入消息; import App from './App';从'./App'导入应用程序;

// Register background handler messaging().setBackgroundMessageHandler(async remoteMessage => { // 注册后台处理程序 messaging().setBackgroundMessageHandler(async remoteMessage => {
console.log('Message handled in the background,'; remoteMessage); console.log('消息在后台处理,'; remoteMessage); }); });

AppRegistry.registerComponent('app', () => App); AppRegistry.registerComponent('app', () => App);

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.要在应用程序侦听来自 Firebase 的通知时也在后台显示通知,您必须创建要显示的本地通知。

I will suggest to use the react-native-fcm library it is very easy to use https://www.npmjs.com/package/react-native-fcm我会建议使用 react-native-fcm 库它很容易使用https://www.npmjs.com/package/react-native-fcm

Following article can give some reasons if everything is configured correctly.如果一切配置正确,以下文章可以给出一些原因。

Why your Push Notifications never see the light of day 为什么您的推送通知从未见天日

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 应用程序被终止时,设备未收到推送通知 - push notification not received to devices when app is killed 应用程序被杀死时 FCM 通知不会触发 - FCM Notification not firing when app is killed 在本机反应中收到 fcm 通知后显示来电 - display incoming call after receiving fcm notification in react native 当应用程序在后台并单击通知时,Android react-native-push-notification onNotification 没有被调用? - Android react-native-push-notification onNotification not called when app is in the background and notification clicked? 第一次安装应用程序时未收到推送通知 - Push notification is not receiving when the app is installed for first time 打开应用程序后如何删除 react-native-firebase/messaging 中的通知标记计数 - How to remove the notification badge count in react-native-firebase/messaging after opening the app 在 ios 上上传图像时,本机应用程序崩溃且没有日志 - react native app is crashing with no logs when uploading image on ios 通过 rest api 在本机反应中通知 - Notification via rest api in react native 如何使用 react-native-firebase/messaging(FCM) 从栏点击通知后将应用程序打开到特定页面? - How to open app to specific page after click notification from bar by using react-native-firebase/messaging(FCM)? 当应用程序被杀死时,有没有办法在 flutter 中接收数据通知? - Is there a way to receive data notifications in flutter when the app is killed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM