我希望你们能够帮助我解决这个从昨晚开始就一直存在的错误,即react-native-push-notification。 我一直在尝试使用
PushNotification.localNotificationSchedule()
功能发送推送通知。在我将 id 属性添加到通知对象之前,代码可以完美运行。
这是代码何时工作的示例:
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 时,它就起作用了。 我期待我的应用程序上显示通知。
根据文档,在创建通道之前通知不会起作用:
"channel-id"
是默认创建的 channelId
,因此在 channelId
中使用它时通知会起作用
您必须在 AndroidMainfest.xml
中硬编码通知通道 ID <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 一起使用并不是它应该使用的用法