简体   繁体   English

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

[英]How to Remove Push Notification in Notification Center after viewed

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

I know this is hack and slash, but you can clear all notifications by changing the badge number on your application. 我知道这是破解和削减,但您可以通过更改应用程序上的徽章编号来清除所有通知

- (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];
    ...
}

If you already had a badge number you don't want to lose (above example will simply clear badge number in the end) you can do something like 如果您已经有一个徽章编号,您不想丢失(上面的例子只是简单地清楚徽章编号)你可以做类似的事情

- (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];
    ...
}

This may not be best practice, but it gets the job done and the client will not know the difference. 这可能不是最佳做法,但它完成了工作,客户不会知道差异。 I like to call it a temp. 我喜欢称之为临时。 fix until I stumble upon a better solution. 直到我偶然发现一个更好的解决方案。 Hope this helps someone! 希望这有助于某人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM