如何在Objective C中使用Local Notification清除iconBadge

时间:2017-04-29 10:42:17

标签: ios objective-c uilocalnotification

我是iOS新手,我遇到有关清晰图标徽章的问题

我的代码就像这样

 UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
        content.title = [NSString localizedUserNotificationStringForKey:@"Elon said:"
                                                              arguments:nil];
        content.body = [NSString localizedUserNotificationStringForKey:@"Hello Tom!Get up, let's play with Jerry!"
                                                             arguments:nil];
        content.sound = [UNNotificationSound defaultSound];

        // 4. update application icon badge number
        content.badge = [NSNumber numberWithInteger:([UIApplication sharedApplication].applicationIconBadgeNumber + 1)];
        // Deliver the notification in five seconds.
        UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger
                                                      triggerWithTimeInterval:5.f
                                                      repeats:NO];
        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
                                                                              content:content
                                                                              trigger:trigger];
        /// 3. schedule localNotification
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            if (!error) {
                NSLog(@"add NotificationRequest succeeded!");
            }
        }]; 

创建本地通知

要清除通知我正在使用此类代码

- (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.
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center removeAllDeliveredNotifications];
    [center removeAllPendingNotificationRequests];
    content.badge = [NSNumber numberWithInteger:([UIApplication sharedApplication].applicationIconBadgeNumber * 0)];


}

但使用此代码徽章并不清楚。如何清除徽章? 在此先感谢!

1 个答案:

答案 0 :(得分:1)

在swift:

func applicationDidBecomeActive(_ application: UIApplication) {

        application.applicationIconBadgeNumber = 0

}

目标C:

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;