反应本机 - 当应用程序被终止/未启动时未收到通知

问题描述 投票:0回答:5

我正在使用 [电子邮件受保护] 进行推送通知。

当应用程序在屏幕上打开时,我会通过

onNotification
收到通知,并根据我的代码收到警报。

问题1 当应用程序在后台(未杀死)时,我会在状态栏中收到通知,但点击它时,它会被关闭并且应用程序不会打开。

我也在 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)

问题2 当应用程序被终止或未启动时,我会在我的 android logcat 中收到此消息:

W GCM :广播意图回调:结果=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000000 pkg=com.mycustomapp(有额外内容)}

就是这样。状态栏或其他任何地方都没有推送通知。

这些是我的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" />

应用程序启动时调用的函数:

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);
});

这是我的 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);

如果完全相关,我使用

react-native-navigation
。如果您需要任何其他信息,请评论。

android react-native react-native-firebase
5个回答
2
投票

无头任务必须注册为 RNFirebaseBackgroundMessage。

更改您的代码:

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

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

我希望您已经完成这 4 个步骤,以在后台列出 FCM 消息。


1
投票

后台和终止状态消息

当应用程序处于后台或Kill状态时,接收消息时不会调用onMessage处理程序。相反,您需要通过 setBackgroundMessageHandler 方法设置后台回调处理程序。

要设置后台处理程序,请尽早在应用程序逻辑之外调用 setBackgroundMessageHandler:

//index.js 从'react-native'导入{AppRegistry};进口 来自“@react-native-firebase/messaging”的消息传递;导入应用程序来自 './应用程序';

//注册后台处理程序 消息传递().setBackgroundMessageHandler(异步remoteMessage => {
console.log('消息在后台处理!',remoteMessage); });

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


0
投票

要在应用程序侦听来自 firebase 的通知时在后台显示通知,您必须创建要显示的本地通知。

我建议使用react-native-fcm库,它非常容易使用https://www.npmjs.com/package/react-native-fcm


0
投票

如果一切配置正确,下面的文章可以给出一些原因。

为什么您的推送通知永远见不到天日


0
投票

我通过添加解决了这个问题:

messaging().onMessage(onMessageReceived);
messaging().setBackgroundMessageHandler(onMessageReceived);

index.js 而不是 App.js 并使用 react-native-notify 库在本地显示通知。

这是我的 index.js 用于 Firebase 通知配置的完整代码。

import notifee, {AndroidImportance, EventType} from '@notifee/react-native';
import messaging from '@react-native-firebase/messaging';

async function onMessageReceived(message) {
const channelId = await notifee.createChannel({
id: 'important',
name: 'Important Notifications',
importance: AndroidImportance.HIGH,
});
console.log('message received', JSON.stringify(message, null, 2));
try {
notifee.displayNotification({
title: message.notification.title,
body: message.notification.body,
android: {
channelId,
importance: AndroidImportance.HIGH,
},
});
} catch (e) {
console.log('error ....... push notification', e);
}
}

messaging().onMessage(onMessageReceived);
messaging().setBackgroundMessageHandler(onMessageReceived);

AppRegistry.registerComponent(appName, () => App);

希望这对您有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.