打开另一个通知后,如何防止通知从通知中心消失?的iOS [英] How to prevent a notification disappear from notification center when another one is opened? iOS

查看:221
本文介绍了打开另一个通知后,如何防止通知从通知中心消失?的iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个接收推送通知的应用程序.此推送通知中,每个通知都包含有价值的信息,这些信息在用户从中打开应用程序时显示.

I'm developing an app that receives push notifications. This push notifications, each one contains valuable information that is showed up when the user opens the app from it.

我的问题是,如果用户收到多个通知,则如果用户点击它并打开应用程序,则所有其他通知将从通知中心消失,而我丢失了所有其他重要信息.

My problem is that if the user receives more than one notification, if the user taps it and open the app, all the other ones disappear from the notification center and I lost all the other important information.

我希望能够保留/防止通知从通知中心消失,以便为用户提供继续从通知中心打开通知的选项.有点像YouTube通知.我什至在Twitch应用通知中看到了这种行为.

I want to be able to leave/prevent the notifications from disappearing from the notification center in order to give the user the option to keep opening them from the notification center. Somehow like YouTube notifications. I even saw that behavior in Twitch app notifications.

有什么主意吗?谢谢.

推荐答案

我知道这是一个很老的问题,但是由于没有答案,我将告诉您如何解决此问题.

I know it's a pretty old question, but since it doesn't have an answer, I'll tell you how I solved this issue.

简而言之,问题是由于将UIApplication.shared.applicationIconBadgeNumber设置为0引起的;正在将所有通知从通知中心中删除.

In short, the issue is caused by setting UIApplication.shared.applicationIconBadgeNumber to 0; it's making all of the notifications to be removed from the notification center.

解决方案是将applicationIconBadgeNumber设置为用户在通知中心拥有的实际通知数.我为此做了一个功能:

The solution is to set the applicationIconBadgeNumber to the real number of notifications the user has in the notification center. I made a function for this:

func updateIconBadge() {
    UNUserNotificationCenter.current().getDeliveredNotifications { notifications in
        DispatchQueue.main.async {
            UIApplication.shared.applicationIconBadgeNumber = notifications.count
        }
    }
}

现在,您可以在AppDelegate.swift中的application(_application:, didFinishLaunchingWithOptions:)applicationWillEnterForeground(_application:)applicationDidBecomeActive(_application:)方法中调用此函数,以确保它在适当的时候更新.

Now you can call this function in the methods application(_application:, didFinishLaunchingWithOptions:), applicationWillEnterForeground(_application:), applicationDidBecomeActive(_application:) in AppDelegate.swift to make sure it will update when it should be.

这篇关于打开另一个通知后,如何防止通知从通知中心消失?的iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆