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

后端 未结 3 2124
借酒劲吻你
借酒劲吻你 2020-12-19 12:30

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 her

相关标签:
3条回答
  • 2020-12-19 12:54

    In order to remove the specific notification from the Notification center automatically,

    • you can't do that from the iOS side, until and unless user interact with that notification
    • But, you can do that from the server side, let me tell you how

    How to remove notification from the notification center programmatically? When server send you any notification via APNS server using the APNS endpoint, the APNS will return you some data as a payload, you will get the notification identifier as a apns-id key in the response header

    Store that apns-id value in your database table at the server side.

    At the end of day or with specific condition criteria, you can delete that notification from the device's notification center by sending another request to APNS by Passing the apns-id value in the request header.

    Skype is doing the same thing.

    Read this for more details: https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html#//apple_ref/doc/uid/TP40008194-CH11-SW1

    0 讨论(0)
  • 2020-12-19 12:58

    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

    0 讨论(0)
  • 2020-12-19 13:09

    In case anyone else ends up here looking for a way to clear notifications without sending a background push, I spent a few hours attempting to replicate the accepted answer, specifically:

    you can delete that notification from the device's notification center by sending another request to APNS by Passing the apns-id value in the request header.

    And it did not work for me at all. As far as I can tell the only way to remove notifications is via a content-available background push. Though I would love to be proven wrong!

    0 讨论(0)
提交回复
热议问题