如何在查看后删除通知中心中的推送通知

时间:2011-11-21 02:05:35

标签: ios ios5 push-notification

点击后是否有办法处理通知中心的推送通知,并在我的应用程序启动后将其删除?

1 个答案:

答案 0 :(得分:6)

我知道这是破解和削减,但您可以通过更改应用程序上的徽章编号来清除所有通知。

- (void)application:(UIApplication*)application didReceiveRemoteNotification (NSDictionary*)payload
{
    NSLog(@"Received notification: %@", payload);
    //swapping between two badge numbers to clear notifications
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    ...
}

如果您已经有一个不想丢失的徽章编号(上面的例子最后只是清除徽章编号),您可以执行类似

的操作
- (void)application:(UIApplication*)application didReceiveRemoteNotification (NSDictionary*)payload
{
    NSLog(@"Received notification: %@", payload);
    /*
     storing current badge number then swapping between 2 values to make sure we 
     clear the badge number. Once this is done set badge number back to original 
     value.
    */
    int badgeNum = [[UIApplication sharedApplication] applicationIconBadgeNumber]
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:badgeNum];
    ...
}

这可能不是最佳做法,但它可以完成工作,客户也不会知道差异。我喜欢称之为临时。直到我偶然发现一个更好的解决方案。希望这有助于某人!