0

I am using push notification in my app, I update the badge count when user tap on the notification and launch the app, but if app is not running and receiving notification continuously its never update the count.

Is there any way to do it in background.

Does iOS provide an api or notification which I can register and handle the badge local like andoroid

Tarun Seera
  • 4,212
  • 4
  • 27
  • 41
  • Yes, is there any way other than this? – Tarun Seera Apr 11 '16 at 13:13
  • Add the code and we will see what are you doing wrong there. – NSNoob Apr 11 '16 at 13:14
  • didReceiveRemoteNotification is working fine its updating too, but if my app is not running then badge is not updating until i open app – Tarun Seera Apr 11 '16 at 13:15
  • Possible duplicate of [Update badge with push notification while app in background](http://stackoverflow.com/questions/14256643/update-badge-with-push-notification-while-app-in-background) – NSNoob Apr 11 '16 at 13:15
  • `didReceiveRemoteNotification` is the correct way to go about it. It will get called whenever you receive a push. You will have to send the badge in your payload. – NSNoob Apr 11 '16 at 13:18
  • Yes agree but for reset I have to create another api to make count 0, In my android app it update a variable via call back whenever a notification occur I am looking for the similar behaviour if IOS provide and I am not aware of it – Tarun Seera Apr 11 '16 at 13:19
  • You can set the badge to 0 on `applicationDidLaunch` and tell your server to update the badge on server-end to 0 accordingly. – NSNoob Apr 11 '16 at 13:21
  • Yes sure but have to open the app for this and that is I already did – Tarun Seera Apr 11 '16 at 13:23
  • To remove the badge you have to open the app. Not to update badge. Badge will get updated when you receive a notification. Just make sure badge count is in push's payload. Just add this line to your `didReceiveRemoteNotification` method. `[application setApplicationIconBadgeNumber:[[[userInfo objectForKey:@"aps"] objectForKey:@"badge"] intValue]];` – NSNoob Apr 11 '16 at 13:25

2 Answers2

0

Usually you would add the new count via the push using:

badge: Int

You would calculate this server side and add it to the payload.

Here is a link to payload params.

Jagat Dave
  • 1,643
  • 3
  • 23
  • 30
Sean Lintern
  • 3,103
  • 22
  • 28
0

Badge count is managed from Server End. For that your backend has to send specific JSON Payload in Push.

{
    "aps" : {
        "alert" : {
            "title" : "Game Request",
            "body" : "Bob wants to play poker",
            "action-loc-key" : "PLAY"
        },
        "badge" : 5
    },
    "acme1" : "bar",
    "acme2" : [ "bang",  "whiz" ]
}

Something like above, in which badge value is 5. When this push will come on iOS it will automatically show this count on App's Badge.

Then next question might be, How to update the Badge then. On app launch, just reset the App badge using

[[UIApplication sharedApplication]setApplicationIconBadgeNumber:0];

at the same time also call a new Webservice to update 0 as badge count on server side so that on next Push Webservice can send 1.Also ask your backedn Guy to increment this count on very push. However this depends on your App cases. The one that i just explained is by considering that on app launch all notifications has to be considered as Read.

Mrug
  • 4,963
  • 2
  • 31
  • 53