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

3

10

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?

Organo answered 12/4, 2019 at 7:2 Comment(0)
W
5

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

Winters answered 11/2, 2020 at 13:53 Comment(4)
Hey this not works. Are you sure of this, silent remote notification not called when application is killed forcefully.Masoretic
of course it doesn't call any method when application is killed but it clears badge and deletes push notification from Notification Center. it perfectly worksWinters
I am working on one app, in which calling functionality is implemented. When user call that time notification is sent to callee. If he is not available to take calls but notification is delivered on his device. that time i want to remove notification from callee side after 30 seconds. I tried your solution but notification not remove. Please help me outMasoretic
First of all send notification with badge (nonzero badge ) when you want to send notification to callee. When you want to remove notification from callee , send silent notification with zero badge ("badge" : 0)Winters
L
4

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

Lard answered 12/4, 2019 at 7:31 Comment(8)
@Organo You can follow my medium profile to learn in-depth about Swift and iOS, here is a link of one of my article about Swift programming medium.com/swift-india/…Lard
Can you please tell me that in which way I should communicate with the APNS server to delete notification? I used to call "/3/device/<device-token>" for sending push notifications. Should I use the same method?Organo
@Organo follow this link: developer.apple.com/library/archive/documentation/… :path - /3/device/<device-token> pass "apns-id" as a header parameterLard
@Organo Also you needs to mark the notification as a silent notification when you are sending for delete, for silent notification use 'content-available' = 1 inside aps{} Also you needs to enable background modes check this: files.readme.io/…Lard
I am able to delete the notifications from the device notification center by sending silent notifications as you suggested. Unfortunately, silent notifications can't perform any action (like deleting a delivered notification) if the app is forcefully terminated by the user. Do you have any suggestion to overcome this issue? Also, I have noticed that apps like Whatsapp and Outlook will delete their push notifications automatically even if the app is forcefully terminated by the user.Organo
@dm_mobileThat is the different scenario when the app is terminated by the user, feel free to post another question for that topic, you should accept my current answer as it works for you, that will help other people to find the proper solution easiylyLard
@HitendraSolanki so you're confirming that your solution only works if the app hasn't been terminated?Operon
@luk2302 Is do we need to code at iOS side ?Threewheeler
G
3

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!

Grommet answered 20/7, 2020 at 18:54 Comment(1)
and those are limited to 2 or 3 per hour, right?Begley

© 2022 - 2024 — McMap. All rights reserved.