简体   繁体   English

如何更新通知徽章编号?

[英]How to update notification badge number?

Sorry as I am jump from android development to IOS development, I would like are there any function that will fire when notification receive? 抱歉,当我从android开发过渡到IOS开发时,我想在收到通知时会触发任何功能吗? or how should I handle the notification? 还是应该如何处理通知?

This is the app delegate in my app. 这是我应用程序中的应用程序委托。 The problem is whenever the message receive, it will not fire the didReceiveNotification function if the app is at the background so how can I update the badge number when the app is at background? 问题是,每当收到消息时,如果应用程序在后台,它将不会触发didReceiveNotification函数,因此当应用程序在后台时如何更新徽章编号? Thanks 谢谢

- (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];
}

Badge count automatically increment when receive push notification in iOS, We no need to update badge count in app, 在iOS中收到推送通知时,徽章计数会自动增加,我们无需在应用中更新徽章计数,

ApplePushService ApplePushService

Push notification format in iOS iOS中的推送通知格式

{
    "aps" : {
        "alert" : "You got your emails.",
        "badge" : 9,
        "sound" : "bingbong.aiff"
    },
    "acme1" : "bar",
    "acme2" : 42
}

Note : "badge" value must as an integer 注意 :“徽章”值必须为整数

badge value automatically incremented from the value of payload. 徽章值从有效载荷的值自动增加。 it change to new value of key "badge" whatever it is. 不管它是什么,它都会变为键“徽章”的新值。

application.applicationIconBadgeNumber = badgeNumber will execute only when your app is running and active not in background mode. application.applicationIconBadgeNumber = badgeNumber仅在您的应用正在运行且未在后台模式下处于活动状态时执行。 So there is no other way instead of sending count in payload for changing badge count when application is not active. 因此,没有其他方法可以在应用程序不活动时发送有效载荷中的计数来更改徽章计数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM