使用频道 ID 时,Expo 通知不起作用

Expo notifications not working when channel id is used

我在 Android 上使用以下代码为我的 expo react 应用程序创建两个频道:

Notifications.setNotificationChannelAsync("default", {
            name: "default",
            importance: Notifications.AndroidImportance.MAX,
            vibrationPattern: [0, 250, 250, 250],
            lightColor: "#FF231F7C",
        })  

Notifications.setNotificationChannelAsync("gameupdates", {
                name: "gameupdates",
                importance: Notifications.AndroidImportance.MAX,
                vibrationPattern: [0, 250, 250, 250],
                lightColor: "#FF231F7C",
            })

频道创建成功,进入Expo应用通知选项也能找到

当我使用 expo's tool 发送没有任何频道 ID 的通知时,它工作正常。但是,当我使用频道 ID“gameupdates”时,通知永远不会到达我的 phone.

知道为什么吗?

我在expo sdk 38.

遵循文档:

If you create a notification and do not specify a channelId, Expo will automatically create a 'Default' channel for you and present the notification through that channel. If, however, you specify a channelId that has not yet been created on the device, the notification will not be shown on Android 8+ devices. Therefore, it's important to plan ahead and make sure that you create all of the channels you may need before sending out notifications.

On devices with Android 7 and below, which don't support notification channels, Expo will remember the relevant settings you created the channel with (in this case, sound: true) and apply them directly to the individual notification before presenting it to the user.

所以我认为如果你想推送指定频道id必须确保一些事情:

  • 您的应用 运行 在设备 8.0+
  • 频道在设备上创建成功,不会影响用户的任何设置(以防检查您 gameupdates 频道)
  • 在 API 26+
  • 上照顾好你的 android 应用程序 运行

祝你好运。

已修复!问题是我需要将 android.useNextNotificationsApi: true 添加到 app.json。 然后我不得不 expo:build 手动 上传构建。

您已成功创建频道,但未更新应用的博览会通知设置。为此,您必须添加 -

android: {
...
"useNextNotificationsApi": true
}

在您的 app.json 文件中。

然后 运行 您的代码通过像 -

一样清除 expo 缓存
expo start -c

并测试一下!您也将开始在 Android 10+ 手机上收到通知。