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

发布于 2024-12-16 21:00:30 字数 46 浏览 1 评论 0原文

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

Is there any way to handle the push notification from the Notification Center after being tap, and remove it when my application has already launched?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紙鸢 2024-12-23 21:00:30

我知道这是黑客攻击,但您可以通过更改应用程序上的徽章编号来清除所有通知。

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

这可能不是最佳实践,但它可以完成工作并且客户不会知道其中的区别。我喜欢称其为临时工。修复直到我偶然发现更好的解决方案。希望这对某人有帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文