【问题标题】:Fetch the integer value of badge(from push notification) : Xcode获取徽章的整数值(来自推送通知):Xcode
【发布时间】:2025-12-23 01:35:15
【问题描述】:

在我的一个应用程序中,我想以编程方式(从推送通知)获取徽章的整数值。

edit:在'application didReceiveRemoteNotification'里面,我要设置:

badge No. = existing badge No. + 1. 

还有,如何找到这个existing badge No.?谁能帮忙...?

提前谢谢...

【问题讨论】:

    标签: objective-c xcode push-notification badge


    【解决方案1】:

    这里对于您要查找的内容有点准确

    appdelegate.m 中执行此操作 ---> 在didfinishlaunching 方法中:

    NSString *badgeCount = [NSString stringWithFormat:@"%li", (long)[[UIApplication sharedApplication] applicationIconBadgeNumber]];
    NSInteger countNumber = [badgeCount intValue];
    

    或者如果你想初始设置为 0:

    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    //this will set 0 to initial badge no 
    

    【讨论】:

      【解决方案2】:
      [UIApplication sharedApplication].applicationIconBadgeNumber
      

      这就是你要找的吗?

      为刚刚到达的 APN 中的徽章编号。

      AppDelegate

      - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
          NSDictionary *notification = [userInfo objectForKey:@"aps"];
          //notification is what you want. print it to find out the details. 
          //Title, Badge Count and all that
      }
      

      【讨论】:

      • 谢谢。但是,我需要一点帮助。在'application didReceiveRemoteNotification'中,我必须设置:徽章编号=现有徽章编号+1。以及,如何找到这个现有的徽章编号?
      • 我不太确定,但我认为它在收到 APN 时会自动设置Badge Number。也许在收到 APN 时将号码保存在NSUserDefault 中,然后在收到另一个时读取它。?