如何清除我的通知标志

How to clear my notification badge

我有一个应用程序,我可以使用 pushbots 向其发送推送通知。用户能够收到通知并单击它 he/she 可以打开应用程序。但是徽章通知仍然显示有通知。我如何将我的通知标记设置为 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" : "increment", ...}

{...,"badge" : 3, ...}

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

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

我建议您将 pushbots 库更新到 1.1 并使用以下方法清除本地和服务器上的标志:

[[Pushbots sharedInstance] clearBadgeCount];

为Swift3

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()
    }

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