gpt4 book ai didi

ios - 如何更新通知角标(Badge)号码?

转载 作者:行者123 更新时间:2023-11-29 02:55:29 25 4
gpt4 key购买 nike

抱歉,因为我从 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)计数,

ApplePushService

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com