7

I am working on application that receives notification and set application badge.
The problem is that when the application is in background state or terminated, the badge count isn't increased.
It remains same.

While when app is in foreground method calls and badge count increased.
I already did that thing from server side,

but I want to know that Is there any method that execute when app is in background or terminated for increasing application badge number?

I already told you I have done this thing as

{
    "aps" : {
        "alert" : "You got your emails.",
        "badge" : 9
    }
}

from server side but what I want is :I dont want to receive that badge count from server payload but I want to increase badge count from my side.

My logic is like that when I receive first notification I saved count in userdeafualts in didReceiveNotification and when I second notification I added count with +1 and when app is in foreground I directly change that badge count as 0 in userdefaults BUT THIS METHOD CANT WORK IN BACKGROUND OR APP TERMINATED

SO I WANT TO KNOW IF THERE IS ANY METHOD THAT EXECUTES IN BACKGROUND OR APP IS TERMINATED SO I CAN INCREASE BADGE COUNT

If anyone knows please help me do it from my side.

Jitendra Modi
  • 2,344
  • 12
  • 34
  • This article could be helpful for you : http://stackoverflow.com/a/14256852/3426053 – boraseoksoon Sep 27 '16 at 08:59
  • It seems that i have to do calculations on server side and receive a count from payload and set as application badge number. but my problem i dont want count from server side. I already mention that this thing is done from server side – Jitendra Modi Sep 27 '16 at 09:02
  • 2
    The best you can do is set `content-available: 1` in the `aps` dictionary and pray. There is no guaranteed way to trigger background execution (whether the app is running or not), except for Push Kit, which is confined to VoIP apps. – Avi Sep 27 '16 at 09:04
  • What i do when application receives notification, I save the count in user defaults and when application receives another notification i just add that count by 1. Now when application enters background or terminated that method didnt call and my count is remain same – Jitendra Modi Sep 27 '16 at 09:05
  • I want just simple method that just execute in background – Jitendra Modi Sep 27 '16 at 09:06
  • 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) – mmmmmm Sep 29 '16 at 11:23
  • Ohh sorry read question carefully. I already write that I did it from server side I want to do it from client side – Jitendra Modi Oct 08 '16 at 05:22
  • @Mark Now check my question and remove that duplicate mark. I think this question is worth in bounty questions – Jitendra Modi Oct 08 '16 at 05:37

3 Answers3

2

Use application(_:didReceiveRemoteNotification:fetchCompletionHandler:) of UIApplicationDelegate. System wakes up your app, if you enabled the remote notifications background mode. For more info check the documentation for this method

Silmaril
  • 4,241
  • 20
  • 22
2

To receive a push notification when your app is in the background you need to update the Capabilities of your Target in Xcode and add Background Modes...Remote Notifications. You must then add the key content-available to the payload and set to 1. This will trigger application:didReceiveRemoteNotification:fetchCompletionHandler:. See the documentation here.

When the app is terminated there is nothing you can do.

Joel
  • 15,654
  • 5
  • 37
  • 60
1

If you will check in AppDelegate.m file, there is a method you can use to let know whether the app is in the background.

- (void)applicationDidEnterBackground:(UIApplication *)application
Daniyar
  • 2,975
  • 2
  • 26
  • 39
objectiveCoder
  • 579
  • 5
  • 17
  • 1
    Sorry to say, but this method only called once when application enter background. I want a method which calls everytime when I receives notification. – Jitendra Modi Sep 29 '16 at 11:06
  • you can make use of remote notification feature in IOS,Check this link this might help you : http://stackoverflow.com/questions/39719528/how-to-handle-ios-remote-notification-while-app-is-in-background – objectiveCoder Sep 29 '16 at 11:10
  • I already told you that i already did this thing from server side. I want to update my badge count from my side. So when i receive notification single method calls in background and update my badge count which is store in userDefault. There is no issue when app is in foreground – Jitendra Modi Sep 29 '16 at 11:14