Ios 如何更新通知徽章编号?
很抱歉,我正在从android开发跳到IOS开发,我想知道有没有收到通知时会触发的功能?或者我应该如何处理通知 这是我的应用程序中的应用程序代理。问题是,每当收到消息时,如果应用程序位于后台,则不会启动didReceiveNotification功能。因此,当应用程序位于后台时,如何更新徽章编号?谢谢Ios 如何更新通知徽章编号?,ios,notifications,apple-push-notifications,appdelegate,Ios,Notifications,Apple Push Notifications,Appdelegate,很抱歉,我正在从android开发跳到IOS开发,我想知道有没有收到通知时会触发的功能?或者我应该如何处理通知 这是我的应用程序中的应用程序代理。问题是,每当收到消息时,如果应用程序位于后台,则不会启动didReceiveNotification功能。因此,当应用程序位于后台时,如何更新徽章编号?谢谢 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)lau
- (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中收到推送通知时,徽章计数会自动增加,我们无需在应用程序中更新徽章计数 iOS中的推送通知格式
{
"aps" : {
"alert" : "You got your emails.",
"badge" : 9,
"sound" : "bingbong.aiff"
},
"acme1" : "bar",
"acme2" : 42
}
注意:“徽章”值必须为整数徽章值从有效负载值自动递增。它会变成钥匙“徽章”的新值,不管它是什么
application.applicationBadgeNumber=仅当您的应用程序正在运行且处于活动状态而不是在后台模式下时,才会执行badgeNumber。因此,当应用程序未激活时,除了在有效负载中发送计数以更改徽章计数之外,没有其他方法可以替代。对不起,我不是必须调用应用程序吗。applicationBadgeNumber=badgeNumber;这样徽章就可以更新了?感谢您在ios中打开应用程序时只更新ApplicationOncBadgeNumber,而不是在后台。抱歉,恐怕我感到困惑,您介意教我,如果我可以创建这样的通知,我应该如何更改代码(问题中的代码)?感谢您通过执行“application.applicationBadgeNumber=0;”,您可以有效地完全删除该徽章。如果您想从应用程序内部设置徽章号码,请使用相同的代码,但更改号码。如果您希望推送通知更改徽章编号,请按照其他答案的说明在有效负载中设置“徽章”:编号。