如何在特定时间从 iOS 通知中心自动删除推送通知? [英] How to automatically delete push notifications from iOS notification centre at a specific time?

查看:30
本文介绍了如何在特定时间从 iOS 通知中心自动删除推送通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用具有推送通知功能的 Swift 开发 iOS 应用程序.该应用通过推送通知(此处使用 APNS)向用户发送生日提醒通知.

I am developing an iOS app using Swift which has push notification feature. The app sends Birthday reminders notifications to the users via push notifications (APNS used here).

目前,推送通知会保留在设备的通知中心,直到用户点击该通知或选择将其从设备中清除.

Currently, the push notification remains in the device's notification center until the user taps on that notification or opt to clear it from the device.

现在我正在寻找一种方法,可以在每天结束时自动删除该推送通知,因为无需在设备中显示过去一天的生日通知.

Now I am looking for a way to automatically delete that push notification at the end of every day as there is no need of showing past day's birthday notifications in the device.

有人可以向我建议一个解决方案,我可以通过哪种方式实现它?是否可以在没有任何用户交互的情况下从 iOS 中删除通知?

Can someone suggest me a solution that in which way I can implement this? Is it possible to delete notification from iOS without having any user interaction?

推荐答案

这适用于应用被用户强行终止的场景:

This for scenario when the app is forcefully terminated by the user :

当您想通过推送通知向用户发送生日提醒通知时,首先发送非零徽章,例如:

First of all send nonzero badge when you want to send Birthday reminders notifications to the users via push notifications , For example :

 {
  "aps": {
    "alert": {
      "title": "Hey! Urgent Reminder",
      "body": "Do not forget my wife SURPRISE BIRTHDAY PARTY"
    },
    "badge": 1
  }
} 

之后,当不需要在设备中显示通知时,您可以发送带有零标记的静默通知,即使应用程序被强行终止也会清除标记和通知strong> 由用户调用,但 didReceiveRemoteNotification 不会调用,因为应用程序已终止.静默推送通知的有效负载:

After that , When there is no need of showing notifications in the device , you can send silent notification with zero badge that will clear badge and notifications even if app is forcefully terminated by the user but didReceiveRemoteNotification will not called because app is terminated. payload for silent push notification :

 {
   "aps" : {
      "content-available" : 1,
        "badge" : 0,
        "Priority" : 10
   }
}

发送该有效负载后,将自动清除徽章并从通知中心删除推送通知.

After send that payload will automatically clear badge and delete push notification from Notification Center.

请注意,如果在发送静默通知之前徽章为零,则不会清除通知.

Note that if badge was zero before sending silent notification , will not clear notifications.

https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html

这篇关于如何在特定时间从 iOS 通知中心自动删除推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆