作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
抱歉,因为我从 android 开发跳到 IOS 开发,我想知道收到通知时是否有任何函数会触发?或者我应该如何处理通知?
这是我应用中的应用委托(delegate)。问题是每当收到消息时,如果应用程序位于后台,它就不会触发 didReceiveNotification 函数,那么当应用程序位于后台时,如何更新角标(Badge)编号?谢谢
- (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];
}
最佳答案
在 iOS 中收到推送通知时,角标(Badge)计数会自动增加,我们无需在应用中更新角标(Badge)计数,
iOS 中的推送通知格式
{
"aps" : {
"alert" : "You got your emails.",
"badge" : 9,
"sound" : "bingbong.aiff"
},
"acme1" : "bar",
"acme2" : 42
}
注意:“角标(Badge)”值必须为整数
关于ios - 如何更新通知角标(Badge)号码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23950620/
我是一名优秀的程序员,十分优秀!