在设备上收到时丢弃推送通知
Discard a push notification when received on device
我正在使用 OneSignal 向 iOS 和 Android 设备发送推送通知。在 iOS 和 Android 设备上收到推送通知时是否可以丢弃它?这不是全部,而是一些基于有效负载数据以及用户是否在后台或前台拥有设备。
更新:
“丢弃”意味着它永远不会显示为通知。
如果我不想打扰用户更新,如果他们已经在前台使用他们的应用程序,但我想在后台显示通知,我有什么选择?
在 OneSignal 中,您想使用 setNotificationWillShowInForegroundHandler
方法来决定是否需要向用户显示通知。
For Android
OneSignal.setNotificationWillShowInForegroundHandler(new NotificationWillShowInForegroundHandler() {
@Override
void notificationWillShowInForeground(OSNotificationReceivedEvent notificationReceivedEvent) {
OSNotification notification = notificationReceivedEvent.getNotification();
// Get custom additional data you sent with the notification
JSONObject data = notification.getAdditionalData();
if (/* some condition */ ) {
// Complete with a notification means it will show
notificationReceivedEvent.complete(notification);
}
else {
// Complete with null means don't show a notification
notificationReceivedEvent.complete(null);
}
}
});
For IOS
let notificationWillShowInForegroundBlock: OSNotificationWillShowInForegroundBlock = { notification, completion in
print("Received Notification: ", notification.notificationId ?? "no id")
print("launchURL: ", notification.launchURL ?? "no launch url")
print("content_available = \(notification.contentAvailable)")
if notification.notificationId == "example_silent_notif" {
// Complete with null means don't show a notification
completion(nil)
} else {
// Complete with a notification means it will show
completion(notification)
}
}
OneSignal.setNotificationWillShowInForegroundHandler(notificationWillShowInForegroundBlock)
注意:更多信息,您可以查看文档。
我正在使用 OneSignal 向 iOS 和 Android 设备发送推送通知。在 iOS 和 Android 设备上收到推送通知时是否可以丢弃它?这不是全部,而是一些基于有效负载数据以及用户是否在后台或前台拥有设备。
更新:
“丢弃”意味着它永远不会显示为通知。
如果我不想打扰用户更新,如果他们已经在前台使用他们的应用程序,但我想在后台显示通知,我有什么选择?
在 OneSignal 中,您想使用 setNotificationWillShowInForegroundHandler
方法来决定是否需要向用户显示通知。
For Android
OneSignal.setNotificationWillShowInForegroundHandler(new NotificationWillShowInForegroundHandler() {
@Override
void notificationWillShowInForeground(OSNotificationReceivedEvent notificationReceivedEvent) {
OSNotification notification = notificationReceivedEvent.getNotification();
// Get custom additional data you sent with the notification
JSONObject data = notification.getAdditionalData();
if (/* some condition */ ) {
// Complete with a notification means it will show
notificationReceivedEvent.complete(notification);
}
else {
// Complete with null means don't show a notification
notificationReceivedEvent.complete(null);
}
}
});
For IOS
let notificationWillShowInForegroundBlock: OSNotificationWillShowInForegroundBlock = { notification, completion in
print("Received Notification: ", notification.notificationId ?? "no id")
print("launchURL: ", notification.launchURL ?? "no launch url")
print("content_available = \(notification.contentAvailable)")
if notification.notificationId == "example_silent_notif" {
// Complete with null means don't show a notification
completion(nil)
} else {
// Complete with a notification means it will show
completion(notification)
}
}
OneSignal.setNotificationWillShowInForegroundHandler(notificationWillShowInForegroundBlock)
注意:更多信息,您可以查看文档。