Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

swift - How to automatically delete push notifications from iOS notification centre at a specific time?

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
  }
} 

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...