如何清除我的通知徽章

时间:2015-03-17 13:30:44

标签: ios xcode notifications push-notification pushbots

我有一个应用程序,我可以使用pushbots发送推送通知。用户能够接收通知,并且在点击它时他/她可以打开应用程序。但是,徽章通知仍然显示有通知。如何将通知标记设置为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

3 个答案:

答案 0 :(得分:0)

您可以在应用程序中调用它:

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

显然这需要更多的工作。例如,如果您正在处理消息,则需要按照读取的消息数减少徽章编号,并确保不会低于零。

您还可以使用"badge"键通过通知有效负载本身修改当前徽章。

例如使用 {..., "badge" : "increment", ...}

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

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

但如果您的应用非常简单,那么只需拨打此电话即可将您的徽章设置为零。

答案 1 :(得分:0)

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

[[Pushbots sharedInstance] clearBadgeCount];

答案 2 :(得分:0)

适用于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];
      }
相关问题