I recently use the Amazon SNS to push notification for my IOS app.
It works well, the only problem I have encountered is when I receive the notification , the badge number will not be updated, here is how I implement:
First I follow the example here https://aws.amazon.com/articles/9156883257507082 Here is the example code from the tutorial.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
application.applicationIconBadgeNumber = 0;
NSString *msg = [NSString stringWithFormat:@"%@", userInfo];
NSLog(@"%@",msg);
[[Constants universalAlertsWithTitle:@"Push Notification Received" andMessage:msg] show];
}
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Register for push notification
application.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
if(launchOptions!=nil){
NSString *msg = [NSString stringWithFormat:@"%@", launchOptions];
NSLog(@"%@",msg);
[[Constants universalAlertsWithTitle:@"Push Notification Received" andMessage:msg] show];
}
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
Message_BoardViewController *boardViewController = [Message_BoardViewController new];
UINavigationController *navigationController = [UINavigationController new];
navigationController.navigationBar.translucent = NO;
[navigationController pushViewController:boardViewController animated:NO];
[boardViewController release];
self.window.rootViewController = navigationController;
[navigationController release];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
[self.window makeKeyAndVisible];
// Logging Control - Do NOT use logging for non-development builds.
#ifdef DEBUG
[AmazonLogger verboseLogging];
#else
[AmazonLogger turnLoggingOff];
#endif
[AmazonErrorHandler shouldNotThrowExceptions];
return YES;
}
As you can see, from the tutorial code it state
application.applicationIconBadgeNumber = 0;
so obviously it will become 0 every time.
======================================================
I wonder what is the standard way to update the badge number?
Which appoarch is correct ?
1) Through programming like this application.applicationIconBadgeNumber = 0;
2) Or from the server payload like this?
$body = array(
'alert' => $message,
'sound' => 'sound.caf',
'badge' => $badge_count // Should be int type
);
========================================================
However, I found there is obstacle for each apporach, for the 1), the didReceiveNotification
is not fire when the app is at background , so I can not do something like application.applicationIconBadgeNumber++;
to update the badge number.
for the 2) , the Amazon SNS service is only return
$body = array(
'alert' => $message,
);
and simply how can the server knows the badge number and add it at server payload, it seems I still need to post the update badge number to Amazon in didReceiveNotification
and add it to payload. But again, it does not call at background.
Sorry for new to IOS progamming, would some experience programmer kindly guide me to implement the badge number update with push notification? Thanks.