【问题标题】:Expo SDK 39 custom channel with local notifications带有本地通知的 Expo SDK 39 自定义频道
【发布时间】:2020-10-13 00:13:13
【问题描述】:

您如何在带有 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?

【问题讨论】:

    标签: android notifications expo


    【解决方案1】:

    Expo forums 上的人指出NotificationRequestInput 包括NotificationContentInputNotificationTriggerInputNotificationTriggerInput 类型的文档包括 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",
          },
        });
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多