带有本地通知的 Expo SDK 39 自定义频道

Expo SDK 39 custom channel with local notifications

如何在带有 Android 的 Expo SDK 39 中使用带有本地通知的自定义频道?

关于通知的主题,Expo 文档似乎包含了针对已折旧版本和当前版本的 Expo 的一大堆说明。

LegacyNotifications 的文档提到“LocalNotification”对象可以包含“channelId”的配置。

而且,事实上,遗留方法就是当前 notification guide 所说的使用方法:

Notifications.presentLocalNotificationAsync({
  title: 'New Message',
  body: 'Message!!!!',
  android: {
    channelId: 'chat-messages',
  },
});

但是,在多个地方,文档说不要使用遗留方法:

“This API is deprecated and will be removed in the SDK 40. Please, check the new notification module.”

“Instead of presentNotificationAsync developers are encouraged to use setNotificationHandler and scheduleNotificationAsync.”

使用当前 API,我可以使用“setNotificationChannelAsync”创建自定义频道“消息”:

if (Platform.OS === 'android') {
    Notifications.setNotificationChannelAsync('messages', {
      name: 'Messages',
      importance: Notifications.AndroidImportance.MAX,
      vibrationPattern: [0, 250, 250, 250],
      lightColor: '#FF231F7C',
    });
  }

文档给出了以下使用“scheduleNotificationAsync”的简单示例:

async function schedulePushNotification() {
  await Notifications.scheduleNotificationAsync({
    content: {
      title: "You've got mail! ",
      body: 'Here is the notification body',
      data: { data: 'goes here' },
    },
    trigger: { seconds: 2 },
  });
}

根据 documentation,“scheduleNotificationAsync”采用的唯一参数是“NotificationRequestInput”,它又可以包含“NotificationContentInput”。但是,我没有看到任何关于 channelId 的提及。

有没有办法在 scheduleNotificationAsync 中使用自定义 channelId?

Expo forums pointed out that NotificationRequestInput includes both NotificationContentInput and NotificationTriggerInput. The documentation for the NotificationTriggerInput 类型的人包括 channelId。

扩展 scheduleNotificationAsync 的示例,channelId 的最简单用法是

  async function schedulePushNotification() {
    await Notifications.scheduleNotificationAsync({
      content: {
        title: "You've got mail! ",
        body: "Here is the notification body",
        data: { data: "goes here" },
      },
      trigger: {
        seconds: 2,
        channelId: "messages",
      },
    });
  }