React native React存储更新后台通知处于killed状态

cbeh67ev  于 2023-10-22  发布在  React
关注(0)|答案(1)|浏览(141)

我正在使用rnfirebase接收react-native上的firebase通知。如果应用程序处于killed状态,我会收到通知,但setBackgroundsocket没有被调用,并且没有相关的任务正在工作。任务,如添加数据到AsyncStorage,增加徽章计数等。

8e2ybdfx

8e2ybdfx1#

尝试使用此示例侦听器,如果问题仍然存在,请附上用于实现通知的代码。也可以尝试使用@notifee以获得更好的体验。

type NotificationsListenersProps = {
   onNotification: (msg: FirebaseMessagingTypes.RemoteMessage) => void;
   onOpenNotification: (msg: FirebaseMessagingTypes.RemoteMessage) => void;
 };

 public createNotificationListeners(props: NotificationsListenersProps) {
    const { onNotification, onOpenNotification } = props;

    /** When the application is running, but in the background **/
    messaging().onNotificationOpenedApp(async remoteMsg => {
      if (remoteMsg) {
        if (!remoteMsg.notification) {
          onNotification(remoteMsg);
        } else {
          onOpenNotification(remoteMsg);
        }
      }
    });

    /** When the application is opened from the quit state **/
    messaging()
      .getInitialNotification()
      .then(async remoteMsg => {
        if (remoteMsg) {
          if (remoteMsg.data) {
            onOpenNotification(remoteMsg);
          }
        }
      });

    /** When application is in quit state **/
    messaging().setBackgroundMessageHandler(async () => {
      await this.incrementBadge();
    });

    /** Foreground messages **/
    messaging().onMessage(remoteMsg => {
      if (remoteMsg) {
        onNotification(remoteMsg);
      }
    });

    notifee.onForegroundEvent(({ type, detail }) => {
      if (type === EventType.PRESS) {
        if (detail.notification?.data) {
          const notification = { ...detail.notification };
          onOpenNotification(notification);
        }
      }
    });

    notifee.onBackgroundEvent(async ({ type, detail }) => {
      const { notification, pressAction } = detail;
      // Check if the user pressed the "Mark as read" action
      if (type === EventType.PRESS && notification) {
        onOpenNotification(notification);
      }
      if (notification && pressAction) {
        if (
          type === EventType.ACTION_PRESS &&
          pressAction.id === 'mark-as-read'
        ) {
          // Decrement the count by 1
          await notifee.decrementBadgeCount();
          // Remove the notification
          if (notification.id != null) {
            await notifee.cancelNotification(notification.id);
          }
        }
      }
    });
  }

相关问题