【发布时间】:2014-06-02 11:52:44
【问题描述】:
我最近使用 Amazon SNS 为我的 IOS 应用推送通知。
效果很好,我遇到的唯一问题是收到通知时,徽章编号不会更新,这是我的实现方式:
首先我按照这里的例子 https://aws.amazon.com/articles/9156883257507082 这是教程中的示例代码。
- (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;
}
如您所见,从教程代码中可以看出
application.applicationIconBadgeNumber = 0;
很明显它每次都会变成0。
================================================ =======
我想知道更新徽章编号的标准方法是什么?
哪种方法是正确的?
1) 通过这样的编程application.applicationIconBadgeNumber = 0;
2) 还是像这样从服务器负载中获取?
$body = array(
'alert' => $message,
'sound' => 'sound.caf',
'badge' => $badge_count // Should be int type
);
================================================ =========
但是,我发现每个应用程序都存在障碍,对于 1),当应用程序处于后台时,didReceiveNotification 不会触发,所以我不能做类似application.applicationIconBadgeNumber++; 的事情更新徽章编号。
对于 2),Amazon SNS 服务只返回
$body = array(
'alert' => $message,
);
服务器如何知道徽章编号并将其添加到服务器有效负载中,看来我仍然需要在didReceiveNotification 中将更新徽章编号发布到亚马逊并将其添加到有效负载中。但同样,它不会在后台调用。
抱歉,IOS 编程新手,请有经验的程序员指导我通过推送通知实现徽章编号更新?谢谢。
【问题讨论】:
标签: ios amazon-web-services notifications push-notification apple-push-notifications