当另一个通知被打开时,如何防止通知从通知中心消失?IOS [英] How to prevent a notification disappear from notification center when another one is opened? 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
}
}
}
现在你可以在application(_application:, didFinishLaunchingWithOptions:)
, applicationWillEnterForeground(_application:)
, applicationDidBecomeActive(_application:)
在 AppDelegate.swift
中以确保它会在应有的时候更新.
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屋!