Dismissing iOS push notifications remotely
Asked Answered
J

4

5

The Gmail app for iOS is able to receive push notifications while the app is not running (as most email apps do).

However, it is also able to clear all Gmail push notifications from the device when the unread count of the user's Inbox becomes zero, even if the app is not running.

Here is an example sequence: 1. Receive a new email in your Gmail account. 2. The iOS device displays a notification for the new message. 3. Go to the Gmail website and open the message (marking the message as "read"). 4. The notification on the iOS device is dismissed.

Note: [[UIApplication sharedApplication] scheduledLocalNotifications] only provides local notifications, i.e. those that were created within the iOS app itself.

As far as Apple's documentation for APNS describes, there is no way to remotely launch an app into the background, and there is no way to dismiss a remote notification.

So, how does the Gmail iOS app make this work?

Judi answered 16/2, 2015 at 22:0 Comment(0)
W
5

I was able to clear all of my push notifications as well by pushing this payload, using Parse. I'm guessing as long as you supply content-available and badge, you should be able to do the same. I didn't have to write any other code in the AppDelegate, but I did have to turn on push notifications in the projects target capabilities.

curl -X POST \
-H "X-Parse-Application-Id: xxxxxxxxxxx" \
-H "X-Parse-REST-API-Key: xxxxxxxxxx" \
-H "Content-Type: application/json" \
-d '{
       "data": {
         "content-available": "1",
         "badge":"0",
         "sound":""
       },
       "where": {"something":"something_else"}
     }' \
https://api.parse.com/1/push
Weedy answered 11/1, 2016 at 18:20 Comment(1)
You are correct - the important part is to set the badge to 0, which clears all remote notifications.Judi
B
2

@styler1972's solution will work for mass Push Notification deletion. If you want to delete a specific one, follow this.

To remove a particular Push Notification (PN ) remotely, you need to store the common apns-collapse-id of that PN in DB. When you want to remove that, just send another silent push notification with the same apns-collapse-id.

To know how to send silent push notification in iOS.

Barytone answered 23/10, 2019 at 16:19 Comment(0)
I
0

There's a 'silent push' feature in iOS that allows your app to wake up and update itself in the background upon receipt of a UI-less push notification.

Session 713 at WWDC 2014 described this at length:

Silent notifications, they are just push payloads that are sent from your APNs server that, instead of presenting a user notification like an alert or a sound or a badge on the screen, iOS, when it receives that push, will instead wake up your app in the background so that your app can do some background image processing or information processing.

In this case, your app is fetching content from a server In this case, your app is fetching content from a server so that the next time the user happens to tap on your app icon and bring it to the foreground, that information is there and ready so nobody has to wait for a loading spinner to complete and all that other stuff.

Interbedded answered 17/2, 2015 at 19:59 Comment(1)
The silent push feature, i.e. the "content-available" flag, only delivers the notification to the app if it is already running, either in the foreground or background. It won't launch the app in response to the notification though.Judi
G
0

Try this if badge number is already set,

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

or try this if not set

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

This would clear your push notifications and local notification.

Goose answered 3/8, 2016 at 16:31 Comment(1)
Note - this will clear all notifications, both remote and local.Judi

© 2022 - 2024 — McMap. All rights reserved.