菜鸟教程小白 发表于 2022-12-12 19:43:43

objective-c - 推送通知角标(Badge)计数未更新


                                            <p><p>这是我的苹果推送通知代码,当应用程序正在运行并且通知到来时,当我在应用程序图标上单击主页按钮时,我正在增加角标(Badge)计数并获得所需的结果。但是当我没有运行我的应用程序并且通知来时,它不会自动增加角标(Badge)计数并且仅保留为 1。值 1 来自服务器。任何人都可以指出我做错了什么。提前致谢。</p>

<pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {   
    userMessageCounter = @&#34;0&#34;;
    postType = 0;
    joinedStreamChecker = 0;
    OwnerValue = 0;
    pushValue = 1;
    badgeValue =0;

    // Override point for customization after application launch.

    // Add the navigation controller&#39;s view to the window and display.
    ;
    ;


    [
   registerForRemoteNotificationTypes:
   (UIRemoteNotificationTypeAlert |
      UIRemoteNotificationTypeBadge |
      UIRemoteNotificationTypeSound)];


    //[ registerForRemoteNotificationTypes:
    //(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

    .applicationIconBadgeNumber = 0;
    // [ registerForRemoteNotificationTypes:(UIRemoteNotificationTypeNewsstandContentAvailability | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert)];

    UIRemoteNotificationType types = [ enabledRemoteNotificationTypes];
    if (types == UIRemoteNotificationTypeNone)
    {
      pushValue = 0;


      NSLog(@&#34;notification off&#34;);
    }

    return YES;
}


- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken1 {

    NSString *str = [NSString
                     stringWithFormat:@&#34;%@&#34;,deviceToken1];
    NSLog(@&#34;%@&#34;,str);

    self.deviceToken = ;
    NSLog(@&#34;dev --- %@&#34;,self.deviceToken);
    self.deviceToken = ;
    self.deviceToken = ;
    self.deviceToken = ;
    NSLog(@&#34;dev --- %@&#34;,self.deviceToken);


}

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {

    NSString *str = ;
    NSLog(@&#34;%@&#34;,str);   

}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    NSLog(@&#34;Received notification: %@&#34;, userInfo);
    //;

    NSString* alertValue = [ valueForKey:@&#34;badge&#34;];
    NSLog(@&#34;my message-- %@&#34;,alertValue);
    badgeValue= ;
    .applicationIconBadgeNumber += badgeValue;
    //badgeValue = .applicationIconBadgeNumber;
    //.applicationIconBadgeNumber=0;
    //[ setApplicationIconBadgeNumber:badgeValue];


}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>Apple 不会为您跟踪您的数据。它只显示你告诉它的内容。因此,您必须将计数存储在服务器上,然后在发送警报时告诉苹果新的角标(Badge)编号。通常这是通过在启动时让应用程序电话回家告诉您的服务器将未读通知计数清零来完成的。</p></p>
                                   
                                                <p style="font-size: 20px;">关于objective-c - 推送通知角标(Badge)计数未更新,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/9742558/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/9742558/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: objective-c - 推送通知角标(Badge)计数未更新