目标c cancelAllLocalNotifications在应用程序处于后台时不起作用

时间:2013-01-30 01:14:26

标签: ios objective-c push-notification

[[UIApplication sharedApplication] cancelAllLocalNotifications];

适用于所有情况,除非,当应用程序在后台运行,然后由用户点击通知启动时,应用程序启动,通知显示,但清除不起作用!

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *notificationPayload = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if(notificationPayload) {
        [self application:application didReceiveRemoteNotification:notificationPayload];
    }
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if([[userInfo valueForKey:@"aps"] valueForKey:@"alert"] != nil) {
        NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
        if(message != nil) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Usage Alert"
            message:message  delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
            [alertView show];
            [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
            [[UIApplication sharedApplication] cancelAllLocalNotifications];
        }
    }

}

0 个答案:

没有答案