Reputation: 2009
Sorry as I am jump from android development to IOS development, I would like are there any function that will fire when notification receive? or how should I handle the notification?
This is the app delegate in my app. The problem is whenever the message receive, it will not fire the didReceiveNotification function if the app is at the background so how can I update the badge number when the app is at background? Thanks
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(switch_tabbar)
name: @"switch_tabbar"
object: nil];
//self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
// self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
[window setRootViewController:tabBarController];
// self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
NSLog(@"%@",@"launch");
application.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
if(launchOptions!=nil){
NSString *msg = [NSString stringWithFormat:@"%@", launchOptions];
//NSLog(@"launch %@",msg);
[self createAlert:msg];
}
NSArray *permissions = [[NSArray alloc] initWithObjects: @"publish_actions", nil];
self.session = [[FBSession alloc] initWithPermissions:permissions];
if (self.session.state == FBSessionStateCreatedTokenLoaded) {
[self.session openWithCompletionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:NO
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error) {
}];
}];
}
return YES;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"%@",@"receive");
application.applicationIconBadgeNumber = 0;
NSString *msg = [NSString stringWithFormat:@"%@", userInfo];
//NSLog(@"receive %@",msg);
[self createAlert:msg];
}
- (void)createAlert:(NSString *)msg {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received" message:[NSString stringWithFormat:@"%@", msg]delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
Upvotes: 0
Views: 885
Reputation: 41
badge value automatically incremented from the value of payload. it change to new value of key "badge" whatever it is.
application.applicationIconBadgeNumber = badgeNumber will execute only when your app is running and active not in background mode. So there is no other way instead of sending count in payload for changing badge count when application is not active.
Upvotes: 0
Reputation: 4901
Badge count automatically increment when receive push notification in iOS, We no need to update badge count in app,
Push notification format in iOS
{
"aps" : {
"alert" : "You got your emails.",
"badge" : 9,
"sound" : "bingbong.aiff"
},
"acme1" : "bar",
"acme2" : 42
}
Note: "badge" value must as an integer
Upvotes: 1