Push Notification Changing Badge of AppIcon - ios

I am working on an app that can receive Push-Notifications.
When a notification is received, I need to change the number of the icon's badge and increment it.
Those notifications are stored in a UITableView in my app.
When a row is pressed, this means that the notification has been read so I want to decrement the badge.
I am new to push notifications and badges and I really couldn't find any efficient way to do it
any solutions ?
Thank you.

You can't automatically increment a badge from a notification. Your notification payload must contain the badge property that will be set to the exact value you want for your badge.
To set the badge value from within your app, you can use:
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:someInteger];
If you want to increment the badge by one every time you receive a notification, you need to keep track of your user's badge server-side.
For example, if you send 3 notifications to an user, you will have a badge column in your database for this user with 3 as a value. If the user opens his app and taps one of the notifications, your app will have to set the badge icon to 2 and send a request to your server to decrease the database value to 2.
TL;DR: There is no such thing as a badge:+1 or badge:autoincrement in a notification's payload. You have to keep track of the badge's value server-side.

Related

How can I make the iOS badge count match the number of notifications in the notifications centre?

My client has two options for how they want the badge count to operate:
Ideal would have been to that the badge count matches the number of notifications in the notification centre associated with the app client-side, but iOS doesn't give you that count, and I can't track it via just counting because they might clear notifications/notifications might expire.
Alternatively, they would like the app not to clear the notifications when you reset the badge count to 0. It appears that iOS also does not give you that option, as resetting the badge count to 0 automatically clears the notification centre for your app.
I'm working in react-native, but could drop into native code to resolve this if necessary.

Event for Push Notification dismissal

I am using the "badge": "+1" attribute in my Arrow Push message to create badge numbers for my app icon in Appcelerator.
When I detect the notification to be clicked from the notification bar, I decrease the badge value by 1, so it reflects the number of currently active notifications. Unfortunately I am unable to find an event for when the user deletes the notifications from the Notification Center without selecting them. This way the badge number can become async to the number of visible notifications.
Is there anything I am missing out on here? Is there perhaps another method to tie the number to the count of active push notifications?

Is it possible to update badge number on app icon without openining app on push notification in iOS?

iOS 8 or 9
When I get push notification in my iOS app I want to update the badge number (count in red) shown on the app icon without opening the app.
Currently it gets updated only and only once the app is opened.
Is that possible?
You need to set a value for the badge key in your push notification payload. Payload Notification
The payload contains information about how you want to notify the user, such as using an alert, badge or sound. It can also contain custom data that you define.
To remove the badge, set the value of this property to 0.

Update badge icon

i try to update badge icon, every time when when the app (in background)receive a push notification.
i used that in "didReceiveRemoteNotification" and "didFinishLaunchingWithOptions"
[UIApplication sharedApplication].applicationIconBadgeNumber++;
If the app is in foreground and receive few push notifications, when the app come back in backgorund the icon's badge is updated with the number of push notifications received.
But if the app is in background the icon's badge is allways 1 like in payload even the app receive many notifications.
Do you have some ideas? Thanks in advance
The push notifications that are sent to your device should have the badge numeric value which will be et automatically when push is received.
The server should somehow handle the number of your unread notifications and it sends you the correct display number in this field.

UILocalNotification - Add 1 to CURRENT icon badge

So I've got a UILocalNotification setup to change the Icon badge.
[myNote setApplicationIconBadgeNumber:[[UIApplication sharedApplication] applicationIconBadgeNumber]+1];
So it works fine if I don't change the badge number after I set it. If the badge shows 2, lets say, when I set the notification, it changes to 3 when the notification goes off. But lets say that I change it from 2 to 5 within the app before the notification fires, it still changes it to 3 when it does fire.
I need it to be able to add 1 to the current badge number, not the number that the badge showed when I first scheduled the notification. Ex: If it's 2 when I schedule the notification, then change it to 5 in the app, the notification should change it 6 when it does fire.
Is there a way to do it? I'm beginning to hate UILocalNotification.
Thanks guys!
Unfortunately, there is no such API. You can, however, discard all your previous notifications and reschedule them with the updated badge number. But I agree with you, local notifications and badge numbers are bound to reach a brick wall and annoy you.

Resources