繁体   English   中英

在 Firebase 云消息传递中,React 本机应用程序未通过 TOPIC 接收通知

[英]React native app not receiving notifications via TOPIC in Firebase cloud messaging

据我所知,我已经使用一些文档配置了 react-native。 The react native app receives notifications properly when selecting the whole app as the target.But I am not able to get the same when sending via topics from the firebase console. 我究竟做错了什么? 提前致谢

通过作为目标的应用程序接收通知的屏幕截图。 [1]: https://i.stack.imgur.com/8huVS.png

代码

PushNotifHelper

    import messaging from '@react-native-firebase/messaging';

    const TOPIC = 'patient-topic';

    export const requestUserPermission = async () => {
      //On ios,checking permission before sending and receiving messages
      const authStatus = await messaging().requestPermission();
      return (
        authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
        authStatus === messaging.AuthorizationStatus.PROVISIONAL
      );
    };

    export const getFcmToken = () => {
      // Returns an FCM token for this device
      messaging()
        .getToken()
        .then(fcmToken => {
          console.log('FCM Token -> ', fcmToken);
        });
    };

    export const receiveNotificationFromQuitState = () => {
      messaging()
        .getInitialNotification()
        .then(async remoteMessage => {
          if (remoteMessage) {
            console.log(
              'getInitialNotification:' +
                'Notification caused app to open from quit state',
            );
          }
        });
    };

    export const receiveBackgroundNotification = () => {
      messaging().onNotificationOpenedApp(async remoteMessage => {
        if (remoteMessage) {
          console.log(
            'onNotificationOpenedApp: ' +
              'Notification caused app to open from background state',
          );
        }
      });
    };

    //stop listening for new messages.
    export const unsubscribeDeviceTopic = messaging().onMessage(
      async remoteMessage => {
        console.log('New notification arrived' + JSON.stringify(remoteMessage));
      },
    );

    export const backgroundThread = () => {
      //It's called when the app is in the background or terminated
      messaging().setBackgroundMessageHandler(async remoteMessage => {
        console.log('Background notification' + JSON.stringify(remoteMessage));
      });
    };

    export const subscribeTopicToGetNotification = () => {
      /**
       * based on Topic, FCM server to send targeted
       * messages to only those devices subscribed to that topic
       */
      messaging()
        .subscribeToTopic(TOPIC)
        .then(() => {
          console.log(`Topic: ${TOPIC} Suscribed`);
        });
    };

在 useEffect 中初始化

    useEffect(() => {
        async function setupPatientNotification() {
          if (await requestUserPermission()) {
            getFcmToken();
          } else {
            console.log('Not Authorization status');
          }
        }
        receiveNotificationFromQuitState();
        receiveBackgroundNotification();
        subscribeTopicToGetNotification();
        backgroundThread();
        return () => {
          unsubscribeDeviceTopic;
          // messaging().unsubscribeFromTopic(TOPIC);
        };
      }, []);

我这边也面临同样的问题,因为我有一个单独的订阅主题,无需等待从 Firebase 检索令牌。

我觉得你需要在系统拿到Fcm Token后或者授权通知权限后,再去订阅topic。

这是我的代码示例,现在可以正常工作了。

useEffect(() => {
    (async () => {
      await createChannelId();
      const authorizationStatus = await requestUserPermission();
      await messaging().registerDeviceForRemoteMessages();
      if (authorizationStatus) {
        const fcmToken = await messaging().getToken();
        setItem(FCM_TOKEN, fcmToken);
        subscribeTopic(NotificationTopics.ApplicationUpdate);
      }
    })();
  }, []);

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM