当我使用 uuidv4() 传递 id 属性时,计划的通知不起作用

Scheduled Notifications don't work when I pass the id attribute with uuidv4()

提问人:Praise codes 提问时间:10/16/2023 更新时间:10/17/2023 访问量:53

问:

拜托,我希望大家能帮助我解决我自昨晚以来一直使用 react-native-push-notification 的这个错误。 我一直在尝试使用该功能发送推送通知。在我将 id 属性添加到通知对象之前,代码运行良好。 下面是代码工作时间的示例:PushNotification.localNotificationSchedule()

PushNotification.localNotificationSchedule({
        channelId: "channel-id",
        title: `Hi there ${fullName}!`,
        message: `You've Got A To-Do "${todoInfo.title}" Now!`,
        date: new Date(todoInfo.dateDue),
        allowWhileIdle: true,
      });

当我这样做时,它也有效:

PushNotification.localNotificationSchedule({
        channelId: "channel-id",
        title: `Hi there ${fullName}!`,
        message: `You've Got A To-Do "${todoInfo.title}" Now!`,
        date: new Date(todoInfo.dateDue),
        id: 555,
        allowWhileIdle: true,
      });

但是当我使用库生成唯一 ID 以便事后可以取消通知时不起作用;此处的示例代码:

import 'react-native-get-random-values';
import { v4 as uuidv4 } from 'uuid';

// Some other code here

PushNotification.localNotificationSchedule({
        channelId: "channel-id",
        title: `Hi there ${fullName}!`,
        message: `You've Got A To-Do "${todoInfo.title}" Now!`,
        date: new Date(todoInfo.dateDue),
        id: uuidv4(),
        allowWhileIdle: true,
      });

拜托,我有办法克服这个问题吗?

我尝试使用像 uuid 这样的随机 id 生成器,但它不起作用。 当我传递硬编码的 id 时,它可以工作。 我期待在我的应用上显示通知。

react-native javascript-objects uuid localnotification react-native-push-notification

评论


答:

1赞 Rayyan 10/17/2023 #1

根据文档,在创建频道之前,通知将不起作用:

"channel-id"是默认创建的,因此在channelIdchannelId

创建本地频道 ID:

您必须在 AndroidMainfest 中对通知通道 ID 进行硬编码.xml

  <meta-data
  android:name="com.dieam.reactnativepushnotification.default_notification_channel_id"
  android:value="@string/default_notification_channel_id" />

有关远程 Firebase Cloud Messenger 通知,请参阅 FCM 文档

检查频道是否存在并列出它们

这是文档中的代码,用于检查频道并列出可用的通知频道

PushNotification.getChannels(function (channel_ids) {
  console.log(channel_ids); // ['channel_id_1']
});

请注意,频道 ID 与通知的唯一 ID 不同,因此尝试将其与 uuid 一起使用并不是它应该使用的用法