首页 > 解决方案 > 本地反应本机推送通知不起作用

问题描述

我在本地 react-native-push-notification遇到问题,如果应用程序已打开且未触发 fcm 通知,则它无法正常工作。这是我的代码...

PushNotification.configure({
      onRegister: function (token) {
        console.log("[LocalNotificationService] onRegister:", token);
      },
      onNotification: function (notification) {
        console.log("[LocalNotificationService] onNotification:", notification);
        if (!notification?.data) { return  }
        // notification.userInteraction = true;
        if(notification.userInteraction){ onOpenNotification(Platform.OS === 'ios' ? notification.data.item ? notification.data.item : notification.data : notification.data) }
        else { onOpenNotification({}) }
        if (Platform.OS === 'ios') {
          // (required) Called when a remote is received or opened, or local notification is opened
          notification.finish(PushNotificationIOS.FetchResult.NoData)
        }
      },
      
      // IOS ONLY (optional): default: all - Permissions to register.
      permissions: {
        alert: true,
        sound: true,
      },

      // Should the initial notification be popped automatically
      // default: true
      popInitialNotification: true,

      /**
       * (optional) default: true
       * - Specified if permissions (ios) and token (android and ios) will requested or not,
       * - if not, you must call PushNotificationsHandler.requestPermissions() later
       * - if you are not using remote notification or do not have Firebase installed, use this:
       *     requestPermissions: Platform.OS === 'ios'
       */
      requestPermissions: true,
    })

    PushNotification.localNotification({
          /* Android Only Properties */
          ...this.buildAndroidNotification(id, title, message, data, options),
          /* iOS and Android properties */
          ...this.buildIOSNotification(id, title, message, data, options),
          /* iOS and Android properties */
          id: `${id}`,
          title: title || "",
          message: message || "",
          playSound: options.playSound || false,
          soundName: options.soundName || 'default',
          userInteraction: true // BOOLEAN: If the notification was opened by the user from the notification area or not
    });

为什么会这样?感谢您对此的帮助,谢谢!

标签: react-native-androidlocalnotificationreact-native-push-notification

解决方案


推荐阅读