使用Expo React Native(Android)监听FCM推送通知传递

3

我遇到了一个与这个问题极其相似的问题。

  • I am using Expo (SDK38) with the Managed Workflow
  • I am creating standalone APK builds with Turtle CLI on CI
  • I have an FCM project working almost perfectly with the standalone app. By almost perfectly I mean:
    • That I am successfully obtaining the device FCM token with the following code:
      import { Notifications } from 'expo';
      
      await Notifications.getDevicePushTokenAsync(); // Gives the token successfully
      
    • That I am sending a push notification when running the following NodeJS script, but:
      const admin = require('firebase-admin');
      
      admin.initializeApp({
          credential: require('./my-credentials.json'),
          databaseURL: 'https://MY_URL_HERE'
      });
      
      admin.messaging.send({
          notification: { title: 'Foo', body: 'Bar' },
          android: { ttl: 86400 },
          token: 'THE_FCM_TOKEN_HERE'
      });
      
      • [Minor issue 1] The device does not show any notification if the app is in foreground;
      • [Minor issue 2] The device shows the notification duplicated if the app is not in foreground.
我已经提到了上面的小问题,但我现在面临的主要问题是我的应用程序根本不会注意到通知已经到达。监听器没有触发。
我尝试过旧版通知模块和新版通知模块:
// Attempt using Legacy Notifications
// https://docs.expo.io/versions/v38.0.0/sdk/legacy-notifications/
import { Notifications as LegacyNotificationsModule } from 'expo';

// Attempt using New Notifications Module
// https://docs.expo.io/versions/v38.0.0/sdk/notifications/
import * as NewNotificationsModule from 'expo-notifications';

LegacyNotificationsModule.addListener(() => {
    // Make any UI change just for we to see it happening
});

NewNotificationsModule.addNotificationReceivedListener(() => {
    // Make any UI change just for we to see it happening
});

// I also tried commenting and uncommenting the code below
NewNotificationsModule.setNotificationHandler({
    handleNotification: async () => ({
        shouldShowAlert: true,
        shouldPlaySound: false,
        shouldSetBadge: false,
    }),
});

请注意,与我在上面链接的类似问题中一样,我使用Expo通知令牌(格式为ExponentPushToken[xxxxxx])。我使用通过Notifications.getDevicePushTokenAsync()获取的标准FCM令牌。请问如何使其正常工作?
1个回答

2
我终于弄明白了。
我已经向Expo发送了一个PR(Pull Request)以改进此文档。
简而言之,引用我在GitHub上的评论

For the app to properly react to the notification, it must come with an extra special field called experienceId. I couldn't find a way to send this field through Firebase Console interface for sending test messages; instead I made it work performing a HTTP POST call with:

  • Headers:
    • Authorization: key=${FIREBASE_SERVER_KEY_TOKEN} (obtained in my Firebase project Settings > Cloud Messaging)
    • Content-Type: application/json
  • Body:
    {
        "to": "<DEVICE_PUSH_NOTIFICATION_TOKEN>",
        "priority": "normal",
        "data": {
            "experienceId": "@anonymous/my-project-slug",
            "title": "Hello",
            "message": "World"
        }
    }
    
一个关键的未记录的信息是,对于像我这样没有expo账户(并且独立构建所有内容)的人,体验ID应该以@anonymous开头。感谢@awinograd在GitHub上找出这个问题

Hi~ Pedro。可以问你一个有关expo通知的问题吗? - cYee
1
@cYee 随时提问! - Pedro A
1
我想我已经找到了问题所在。本来想问一下你有关 FCM 中 senderId 不匹配的问题,但是后来发现无法使用 Expo 客户端与设备令牌不匹配,只能使用独立构建版本。谢谢 Pedro。 - cYee
嘿,@PedroA 我正在使用来自Firebase的Web配置,但我的API令牌是针对Android的。我无法接收消息。POST方法返回200。 - Ayush Kumar
@AyushKumar 你好,很抱歉,我所了解的都在这个页面和链接中了...祝你好运 - Pedro A

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接