在设备上收到时丢弃推送通知

发布于 01-20 13:38 字数 232 浏览 2 评论 0原文

我正在使用 OneSignal 向 iOS 和 Android 设备发送推送通知。在 iOS 和 Android 设备上收到推送通知后是否可以将其丢弃?这不是全部,而是一些基于有效负载数据以及用户是否在后台或前台运行设备。

更新:

“丢弃”意味着它永远不会显示为通知。

如果我不想通过更新打扰用户(如果他们已经在前台使用其应用程序,但我想在后台显示通知),我有什么选择?

I'm using OneSignal to send push notifications to iOS and Android devices. Is it possible to discard a push notification when it's received on a iOS and Android device? It's not all of them, but some based on the payload data and if the user has the device in the background or foreground.

Update:

"discard" meaning that it is never shown as a notification.

What are my options when I don't want to disturb users with updates if they are already in foreground using their app, but in background I like to show the notification?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生死何惧 2025-01-27 13:38:38

onsignal 中,您需要使用setNotificationwillshowinforegroggnhandler方法来决定是否需要向用户显示通知。

对于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);
    }
  }
});

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)

注:有关更多信息,您可以检查文档。

https://documentation.oneignal.com/ doc/sdk-notification-event手柄#前景 - 通知重新认识的事件

In OneSignal you want to use setNotificationWillShowInForegroundHandler method to decide whether the need to show a notification to the user or not.

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)

Note: For more info, you can check the documentation.

https://documentation.onesignal.com/docs/sdk-notification-event-handlers#foreground-notification-received-event

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文