Ios 如何清除我的通知徽章

Ios 如何清除我的通知徽章,ios,xcode,notifications,push-notification,pushbots,Ios,Xcode,Notifications,Push Notification,Pushbots,我有一个应用程序,我可以使用推送机器人发送推送通知。用户能够收到通知,单击该通知即可打开应用程序。但是,徽章通知仍然显示存在通知。如何将通知标记设置为0 这是我的appdelegate.m - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Register for Remote Notifications

我有一个应用程序,我可以使用推送机器人发送推送通知。用户能够收到通知,单击该通知即可打开应用程序。但是,徽章通知仍然显示存在通知。如何将通知标记设置为0

这是我的appdelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Register for Remote Notifications
    [Pushbots sharedInstanceWithAppId:@"5503e09a1d0ab1481f8b45a1"];
    NSDictionary * userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if(userInfo) {
        // Notification Message
        NSString* notificationMsg = [userInfo valueForKey:@"alert"];
        // Custom Field
        NSString* title = [userInfo valueForKey:@"title"];
        NSLog(@"Notification Msg is %@ and Custom field title = %@", notificationMsg , title);
    }
    return YES;
}

-(void)onReceivePushNotification:(NSDictionary *) pushDict andPayload:(NSDictionary *)payload {
    NSString* message = [pushDict valueForKey:@"alert"];
    UIAlertView *alertMessage = [[UIAlertView alloc] initWithTitle:@"New Event !" message:message delegate:self cancelButtonTitle:@"Open" otherButtonTitles: @"I will check later",nil];
    [alertMessage show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    Pushbots * pushbots = [Pushbots sharedInstance];
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"Open"]) {
        [pushbots OpenedNotification];
    }
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

您可以在应用程序中调用此选项:

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
显然,这需要更多的工作。例如,如果您处理的是消息,则需要按读取的消息数减少徽章号,并确保不低于零

您还可以使用
“badge”
键通过通知负载本身修改当前的badge

例如使用
{…,“徽章”:“增量”,…}

{…,“徽章”:3,…}

第一个版本是我猜最常见的版本


但是,如果你的应用程序相当简单,那么只需调用它,你的徽章就会设置为零。

我建议你将pushbots库更新为1.1,并使用以下方法在本地和服务器上清除徽章:

[[Pushbots sharedInstance] clearBadgeCount];

适用于Swift 3

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
    {
    UIApplication.sharedApplication().applicationIconBadgeNumber = 0
    Pushbots.sharedInstance().clearBadgeCount()
    }

func applicationWillEnterForeground(application: UIApplication)
    {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.  
    UIApplication.sharedApplication().applicationIconBadgeNumber = 0
    Pushbots.sharedInstance().clearBadgeCount()
    }
目标C

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
     {
     [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
     [[Pushbots sharedInstance] clearBadgeCount];
     }

- (void)applicationWillEnterForeground:(UIApplication *)application 
     {
      // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

      [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
      [[Pushbots sharedInstance] clearBadgeCount];
      }

这是正确的答案,因为它清除了服务器上的计数。在Swift上,在AppDelegate.app上使用此didFinishLaunchwithOptions:Pushbots.sharedInstance().clearBadgeCount();