【问题标题】:How to clear my notification badge如何清除我的通知徽章
【发布时间】:2015-03-17 13:30:44
【问题描述】:

我有一个应用程序,我可以使用推送机器人向其发送推送通知。用户能够收到通知并单击它,他/她可以打开应用程序。但是,徽章通知仍然显示有通知。我如何将我的通知徽章设置为 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

【问题讨论】:

    标签: ios xcode notifications push-notification pushbots


    【解决方案1】:

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

    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    

    显然,这需要更多的工作。例如,如果您正在处理消息,则需要将徽章编号减去已阅读消息的数量,并确保不低于零。

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

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

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

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

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

    【讨论】:

      【解决方案2】:

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

      [[Pushbots sharedInstance] clearBadgeCount];
      

      【讨论】:

      • 这是正确的答案,因为它清除了服务器上的计数。在 Swift 上,在 AppDelegate.app 上使用这个 didFinishLaunchingWithOptions:Pushbots.sharedInstance().clearBadgeCount();
      【解决方案3】:

      对于 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];
            }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多