Informing APNS about badge count - ios

I've implmented a simple server for pushing notifications to iOS devices and I can't imagine how to handle badges. I've read some topics and all I know that it's not a good solution to handle them manually on device, that it's the server job...
Everything is clear but I still don't know and couldn't find anywhere how I to let the server know that there are some unreaded notifications on my device? I mean where in UIApplicationDelegate is info about beeing read?
Just to help you guys imagine my problem:
Application is running in foreground and receives push notification.
User doesn't enter app so the badge is visible.
After 5 mins comes another notification (again with badge value set to 1 in payload).
And this scenario could happen many times but the APNS is still pushing payload with the same value for badge.

The number of badges can be obtained from method
[application applicationIconBadgeNumber]
inside application:didFinishLaunchingWithOptions: method.
Hope this helps!
UPDATE:
After clarifying the question, I'll update the answer with accepted solution.
For future searchers:
what if the backend would increase the badge every time when the notification will be sent? for example, You send first notification with badge count 1. Then it's time to send another notification - you send it with badge count 2. Then user opens the app and makes API call to the backend - and you zero-out the badge count, and the next notification would be send with badge count 1. If app get's notification in foreground, it makes the api call immediately and you zero-out the badge count immediately as well.

Related

Counting push notifications on iOS (deal with cancel from notif center)

I'm trying to understand on how to deal with a particular use case with push notifications.
Let's suppose that I have 4 push notifications in the notification center waiting for an interaction, the server keeps track of the badge number. The user picks up one, in the -application:didReceiveRemoteNotification: I decrement the badge and communicate to the server the notification back to make it decrements its "badge" number and mark the notification as read.Everything works fine.
I'm not able to deal with that case. Let's say that the user deletes from the notification center one notification and then opens the app by tapping on another, I will have an incorrect badge number on both server and app, since deleting a notification it doesn't seem to affect the badge number.
Is there a way to understand which remote notification has been removed?
It seem that for a more fine grained control is better to use a mix of silent push and remote notification.

How to increment ios badge notifiation count when app is not running?

We are already done with the initial setup for implementing Push notifications and are able to send/receive notifications successfully.
Now, we have been looking for the possible solutions to handle badge count increment from ios app (especially when app is not in foreground) -
1. Delegate the badge count calculation to server side.
- If we think of offloading the task of badge calculation to app server, then question arises is that, how server side will get to know about the count of viewed notifications so that badge can be decremented accordingly.
2. Use UIBackgroundModes and call 'didReceiveRemoteNotification:fetchCompletionHandler' method.
- Apple says, this method will get called when my app is either in foreground or background state. With this we should be able to track the count/notification info even when the app is in background & thus it probably enables us to do the necessary calculation in iOS app itself.
However, we have noticed that this method doesn't invoke when app is in background. Does it work only for iOS Silent notifications.
Any guidance/help in this regard will be greatly appreciated.
Thanks.
Option 1 is done by most apps (WhatsApp, Mail). It means you have to send a lot of 'notification read' messages to the server.
As for option 2, it works only for silent notifications (as you already noted) and in my experience, it is not entirely reliable. There even seems to be a rate limit on these.

Cancel Local Notifications after app is unused for a certain period of time

I am wondering if it's possible to do what the titles says. I have an application that has a refill reminder to refill your prescription drug via local notifications. I have seen that some apps (pill reminder apps mostly) push a notification if you have not taken your pill, or have not answered back to that notification, and was wondering if I can do the same if a user doesn't open/interact with the app after a certain period of time.
I have not began implementation but have thought about this thoroughly. What I am thinking of doing is having some sort of flag when the app is opened that removes that local notification and sets a new one once the app has gone in the background/inactive. The local notification would be set to three months from when the app has gone in the background/inactive. The question then becomes, how do I handle canceling all notifications after this notification has been received, regardless of whether the user opens the app at that notification or not?
If the user opens the app on that notification, I can have a check the method application:didReceiveLocalNotification and then handle the case where that local notification has been set and then use [[UIApplication sharedApplication] cancelAllLocalNotifications]
But if the user does not tap or open the app, how can I check and cancel all local notifications?
Sorry if this is a bit long or worded weirdly (sorry not good with words and explaining things). Let me know if you need more info or better explanation. Thanks in advance!
If I understand your question correctly, you want to know how to keep notifications from repeating when the user does not respond to a notification by opening your app.
You might consider configuring your local notification not to repeat. Instead, you might reschedule notification batches each time the application is launched.
Alternatively, if your application has a server-side component, you can use push notifications on iOS 7+ to wake your app, briefly. There is no equivalent to this behavior using UILocalNotification.

How to handle badge count If user delete/clear the notification from the server in ios?

Currently I am implementing an ios app with the server push notification.
I am successfully able to send push notifications with badge.
Badge count will be incremented from the server. If user opens the application from notification, it will reset count on the server.
Now the query is if user is not opening application from notification and deleting/clearing all notifications.
That means there will be no notification so when app will be opened from the background it wont find any notification and so it will not reset the count on server and also not the badge value on app icon.
So how can i resolve this issue?
Use custom notification icon and maintain read and unread counts with in the app,you will not manage badge counts from push notifications when user remove and open notifications in the app.
From what I understand, the only way your app is getting the relevant data is if the user clicks on the notification. I'm assuming you're sending a custom payload of some kind. Though I don't know the nature of your app at all, I would say this is really not the way to go specifically because of the problem you're encountering - you have no way of knowing if/when the user clears notifications in the Notification Center and if they do, that data is forever lost to your app.
I would recommend your app request the new data from your server upon launch - ie/ get anything new since last time the app was opened - this way it's independent of whether the push was received or not and depending on what your server sends back, you can clear the push count (or not).
Whenever application hits appDidBecomeActive delegate, clear the badge count to zero.

Deleting notifications from the notification center revisited

The previous posting on here regarding deleting notifications from the notification center claim its not possible to delete individual notifications, only all of them.
However individual notifications do get deleted for the reminder app - set 3 reminders to fire in a couple of minutes, when they fire go to the notification center, now select one, after the reminder app launches go back to the notification center and that specific notification has been deleted but others remain. So how is this achieved?
The Reminders app probably fires local notifications. Local notifications can be withdrawn, using cancelLocalNotification: on UIApplication.
(Additionally, push notifications when sent using the enhanced call (first byte is 1) supports an expiry parameter (when sending, not inside the JSON payload) that is supposed to mean that this notification, if not delivered by a certain date, should not be delivered. It is possible that this parameter is also used in a similar way to hide received notifications.
It is also highly possible that Apple's own apps do whatever the hell they want.)
When the user taps on the notification:
If the app was running in the background, you retrieve it using AppDelegate's method didReceiveLocalNotification.
If it wasn't running, then the notification can be obtained with the didFinishLaunchingWithOptions method. You just need to search the launchOptions dictionary for the UIApplicationLaunchOptionsLocalNotificationKey.
If you want to delete specific notifications which have already fired, specially when the user doesn't enter the app by tapping on the notification, then it's probably better to store them in NSUserDefaults so that you can still obtain them later. That approach is explained here.

Resources