我在我的应用程序中使用推送通知服务。当应用程序在后台时,我能够在通知屏幕上看到通知(当我们从iOS设备顶部向下滑动时显示的屏幕)。但如果应用程序是在前台的委托方法
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
正在被调用,但通知屏幕上没有显示通知。
我想在通知屏幕上显示通知,不管应用程序是在后台还是前台。我厌倦了寻找解决办法。任何帮助都非常感激。
当应用程序在前台显示横幅消息时,使用以下方法。
iOS 10, Swift 3/4:
// This method will be called when app received push notifications in foreground
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler([.alert, .badge, .sound])
}
iOS 10, Swift 2.3:
@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void)
{
//Handle the notification
completionHandler(
[UNNotificationPresentationOptions.Alert,
UNNotificationPresentationOptions.Sound,
UNNotificationPresentationOptions.Badge])
}
你还必须将你的应用委托注册为通知中心的委托:
import UserNotifications
// snip!
class AppDelegate : UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate
// snip!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// set the delegate in didFinishLaunchingWithOptions
UNUserNotificationCenter.current().delegate = self
...
}
最好的方法是通过使用扩展AppDelegate: UNUserNotificationCenterDelegate在AppDelegate中添加UNUserNotificationCenterDelegate
该扩展告诉应用程序能够在使用时获得通知
并实现这个方法
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(.alert)
}
只有当应用程序在前台时,才会在委托上调用此方法。
最后的实现:
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(.alert)
}
}
要调用这个,你必须在AppDelegate中在didFinishLaunchingWithOptions中设置委托添加这一行
UNUserNotificationCenter.current().delegate = self
你可以修改
completionHandler(.alert)
与
completionHandler([.alert, .badge, .sound]))
添加completionHandler行委托方法为我解决了同样的问题:
//Called when a notification is delivered to a foreground app.
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .badge, .sound])
}
也适用于iOS 14+,不需要手动处理任何警报或视图,只需让iOS做它的事情
在里面,我们重新收到通知替换如下代码
func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler:
@escaping (UNNotificationPresentationOptions) -> Void
) {
if (UIApplication.shared.applicationState == .inactive || UIApplication.shared.applicationState == .background) {
if #available(iOS 14.0, *) {
completionHandler([[.banner, .sound]])
} else {
completionHandler([.alert, .sound])
}
} else {
if #available(iOS 14.0, *) {
completionHandler([[.banner]])
} else {
completionHandler([.alert])
}
}
}
要注意的主要事情是不要使用声音,而在前景,它不会显示横幅