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.
You are supposed to send the badge number in the payload of the push notification. Your server should set the badge count. This way the badge count will be updated regardless of whether the app is running or not running (and regardless of whether the user taps the notification to open the app or not). Your server should know what number to send.
And as to how your server is supposed to know what badge number to send - I don't know your app's logic, but lets take an email application as an example - suppose you want the badge number to show how many unread messages exist. The app should notify the server whenever a message is read, so the server can maintain the correct badge number for each user. This way, even if the user has multiple ways to access the data (iPhone app, iPad app, desktop browser, etc...), the server would know the current badge count and each device would show the correct badge count.
The application.applicationIconBadgeNumber = 0
is meant to clear the badge once the application is opened. It should be done in both didReceiveRemoteNotification
and didFinishLaunchingWithOptions
, so that the badge number will always be cleared when the application is launched, or when the push notification is handled by the app.
Thanks for your reply, that means I should use approach 2) metioned in my question. But how can I post the badge number to the server? As the didReceiveRemoteNotification is not run at the background. To be precise, where should I put the post badge number function? Thanks
@user782104 You should post the badge number update to your server when the user taps on the notification or taps the app icon to launch the app, since in either case you'd want to notify the server that all messages were viewed by the user. Therefore you should do it in both
didReceiveRemoteNotification
anddidFinishLaunchingWithOptions
.sorry, I am afraid I am confused. if it update only when user tap on either notification / app icon, what if there is two or more notification when the app is at background? then it will not be update? thanks
@user782104 If the user doesn't interact with your app, the number should only increase as the server sends notifications with increased badge number (due to new data becoming available at the server) or decrease if the server sends notifications with decreased badge number (if the user views the data via some other device). Only when the user views the notifications via your app (for which purpose the user has to tap the notification/app icon) you should clear the badge and notify the server to reset the badge number for that user.
how do I notify the server, do I send a POST request with the new badge count, once the push notification is viewed?