我在我的Flutter应用程序上集成了onesignal通知,一切都按预期工作,但我收到了重复的通知。具体来说,当应用程序打开时(在前台),我会在应用程序中收到通知并显示一个小部件,但我也会收到与手机通知相同的通知。
下面是在前台接收通知的代码
// this method is being called in main()
// show notification when the app is opened
OneSignal.shared
.setNotificationWillShowInForegroundHandler(_localNotificationsHandler);
static _localNotificationsHandler(OSNotificationReceivedEvent event) {
AppSnackbar.openNotificationsSnackbar(
title: event.notification.title ?? '',
msg: event.notification.body ?? '',
);
}
这是推送通知的代码
static Future<void> sendNotification({
required List<String> tokens,
required String title,
required String message,
}) async {
try {
var notification = OSCreateNotification(
playerIds: tokens,
content: message,
heading: title,
);
await OneSignal.shared.postNotification(notification);
} catch (e) {
print(e);
}
}
1条答案
按热度按时间6vl6ewon1#
解决方案是在处理应用程序上收到的通知后添加这行代码