changing the icon badge number when local notification fires - iphone

I need to increase the icon badge number everytime a local notification gets fired to inform the user that there is one more question he needs to answer. When the user answers a question of any sent notification the icon badge number is beeing reduced by 1. I only have one-time notifications, no recurring.
Since the app is not running when the notification fires I do not have any idea how to increase the icon badge number at that time. While there is no problem to decrease the number when the user answers a question - since this is done within the app.
Is there any possibility - maybe similar to registerForRemoteNotificationTypes for push notifications?
Many thanks!

The UILocalNotification class has a property applicationIconBadgeNumber of type NSInteger, which you can set, when scheduling the local notification.

Related

iOS: Notification Badge Number does not reset

I want to reset the notification badge number whenever the app is opened. This works perfectly fine calling
application.applicationIconBadgeNumber = 0
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
However, when I one ore more new notifications come in, the icon badge number again goes to 42 no matter how many notifications come in.
Does anyone know how to fix this?
You can follow one of the following for achieving this,
You can store notifications received in your database and when user opens app, you can get the count of unread notifications and update the count.
If you are targeting single users then, you can get the delivery of Push notifications using https://stackoverflow.com/a/50044201/5084797 and then when the notification is opened you can pass notification Open event to server. So the next time the server sends push notification, they can just check for the difference in Deliver and Open and send that count in badge. If user has cleared all notifications from Notification centre without opening them. In that case as soon as App opens you need to update that at server, Else you might be getting wrong count from server.

UILocalNotification shows 400 badges

I am scheduling a UILocalNotification for an interval. Basically I wanted a notification to be triggered every x days on this app. So the way I did it is to schedule a bunch of notifications at once. So say it's every 2 days, then I do a calculation of the dates every 2 days from now for a year. I am not sure if this is the right and most efficient solution to it, but from researching online, this is what I get I need to do.
The issue is that when the notification fires and I open the app, the badges count goes up to 500, which I think is the number of all future notifications I've scheduled. How is this even possible> Shouldn't the badge only show the number of notifications before the timestamp?
The badge number does not show a number of notifications, but the badge value that you set as notification payload.

Handle APNS Remote Notifications while in Background

I have implemented all recommended methods in AppDelegate to get working Remote Notifications service.
I can accept them while running, while launching and while turned off.
But there is an issue, since I can't work with many received notifications while in background. I can work only with latest notification.
What is recommended manual to do that? How can I got all notifications received while in background? Is it only solvable via manual call to my service provider (sender of apns data)?
With all the projects I've worked on there hasn't been a way to locally store this information if the push notification is dismissed. In all those cases we used a small file on the server that the app would connect to and pull when it became active again. There was also some place in the app where the user could see all their notifications which, again, were stored on the server for quick retrieval.
With the way I understand push notifications to be setup, if the notification is dismissed the system discards it. It'll perform anything it's supposed to do (such as update the badge number and play the correct sound) but any additional information specific to that notification is lost.
Not sure if this helps, but if you just want to know how many notifications you have missed while you were in background. You can create a variable which contains notification number and store this in the app every time you handle notification. When you come out of background and receive a new notification you can subtract the new number with the stored number to find out the number of missed notifications. I don't think there is a way where iOS can give you complete data associated with all the notification device have received while the app was in background.
The best solution is to keep a list of sent notifications with all relevant data on your server, so the app can access that data when it launches. Sending multiple notifications with data that is not stored on the server can be risky, because the application only receives the notification when the user opens the app from that notification, so if they tap on one notification, the app will only every receive that one.
If you have them all in a list on your server, the app can simply go and pull that list down, and process it, making sure no data is lost.

What happens when user does not respond to Push Notification?

Lets assume a user receives a notification. The user did not respond to notification. what happens? will it automatically get closed? if yes, what is the duration for the expiry of notification on the device? Thanks in advance.
The notification will not automatically get closed, but if another one comes in—be it a text message, a voicemail, or even a notification from another app—then your notification will effectively vanish. If the notification payload includes a badge number for your app, then that will get updated; aside from that, though, once another notification supersedes it, yours is gone.
To your app the behaviour will be the same as if he tapped the close button.
If the user doesn't respond, your app will never know. The push message will remain on the screen until the phone is unlocked or some other message is received (SMS or other push notification)

Is there a way to leave previous messages up while pushing out badge updates?

So imagine this scenario:
10.00: your app pushes a message "Hello"
10.01: your app pushes a badge update with no message out the same device
What happens is that the message dissappears. So if the user didn't see it, it's gone. Is there a way to send a badge notification without clearing any previous messages? I know you can send the message again, but I don't want to spam users who may have already ready the message.
I don't want to have a discussion about the why, simply if it's possible?
Unfortunately, it is not possible. We recommend including badge updates with your alerts, to set it at the same time, but there's no way to just update the badge without "overwriting" the alert.
At the same time, however, any message from any other application will also overwrite your earlier alert.
If you include a sound in your APNS payload, the phone will either play the sound or vibrate, so you could at least give the user feedback that a message has been received if you end up needing to do this.