【问题标题】:Listen to FCM push notification delivery with Expo react native (android)使用 Expo react native (android) 收听 FCM 推送通知传递
【发布时间】:2020-12-14 12:42:34
【问题描述】:

我面临一个与this one 极为相似的问题。

  • 我正在将 Expo (SDK38) 与托管工作流一起使用
  • 我正在使用 Turtle CLI on CI 创建独立的 APK 版本
  • 我有一个 FCM 项目几乎可以与独立应用程序完美配合。 几乎完美我的意思是:
    • 我使用以下代码成功获取了设备 FCM 令牌:
      import { Notifications } from 'expo';
      
      await Notifications.getDevicePushTokenAsync(); // Gives the token successfully
      
    • 运行以下 NodeJS 脚本时,我正在发送推送通知,但是:
      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'
      });
      
      • [小问题 1] 如果应用在前台,设备不会显示任何通知;
      • [小问题 2] 如果应用不在前台,设备会显示重复的通知。

为了完整性,我已经提到了上面的小问题,但我现在面临的主要问题是我的应用程序只是不会注意到通知到达。侦听器不会触发。

Legacy Notifications ModuleNew Notifications Module 我都试过了:

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

回想一下,就像在 the similar issue I linked above 中一样,我使用 Expo 通知令牌(形式为 ExponentPushToken[xxxxxx])。我正在使用通过Notifications.getDevicePushTokenAsync() 获得的标准 FCM 令牌。

我怎样才能做到这一点?

【问题讨论】:

    标签: android react-native push-notification firebase-cloud-messaging expo


    【解决方案1】:

    我终于明白了。

    我已经发送了PR to Expo 来改进这方面的文档。

    简而言之,引用my comment on GitHub

    为了让应用正确响应通知,it must come with an extra special field called experienceId。我找不到通过 Firebase 控制台界面发送此字段以发送测试消息的方法;相反,我让它执行HTTP POST 调用:

    • 标题:
      • 授权:key=${FIREBASE_SERVER_KEY_TOKEN}(在我的 Firebase 项目中获得 Settings > Cloud Messaging
      • 内容类型:application/json
    • 身体:
      {
          "to": "<DEVICE_PUSH_NOTIFICATION_TOKEN>",
          "priority": "normal",
          "data": {
              "experienceId": "@anonymous/my-project-slug",
              "title": "Hello",
              "message": "World"
          }
      }
      

    没有记录的关键信息是,对于像我这样没有 expo 帐户(并且正在独立于 expo 构建一切)的人,体验 ID 应以 @anonymous 开头。 Full credits for @awinograd in GitHub for figuring this out.

    【讨论】:

    • 嗨~佩德罗。介意我问你一个关于展会通知的问题吗?
    • @cYee 随时提问!
    • 我想我已经发现了这个问题。想问你关于 fcm 中的 senderId 不匹配的问题。但发现它无法使用带有设备令牌的 expo 客户端,而是独立构建。谢谢佩德罗。
    • 嘿,@PedroA 我正在使用来自 firebase 的 Web 配置,但我的 API 令牌是 android。我无法接收消息。 post 方法返回 200。
    • @AyushKumar 你好,对不起,我知道的一切都在这个页面和链接上......祝你好运
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 2022-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-10
    • 1970-01-01
    相关资源
    最近更新 更多