我在我的应用程序中使用推送通知服务。当应用程序在后台时,我能够在通知屏幕上看到通知(当我们从iOS设备顶部向下滑动时显示的屏幕)。但如果应用程序是在前台的委托方法

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo

正在被调用,但通知屏幕上没有显示通知。

我想在通知屏幕上显示通知,不管应用程序是在后台还是前台。我厌倦了寻找解决办法。任何帮助都非常感激。


当前回答

添加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])
} 

其他回答

最好的方法是通过使用扩展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]))

当应用程序在前台显示横幅消息时,使用以下方法。

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
      ...
   }

Objective - C

对于iOS 10,我们需要集成willPresentNotification方法来在前台显示通知横幅。

如果应用程序处于前台模式(活动)

- (void)userNotificationCenter:(UNUserNotificationCenter* )center willPresentNotification:(UNNotification* )notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
    NSLog( @"Here handle push notification in foreground" ); 
    //For notification Banner - when app in foreground
    
    completionHandler(UNNotificationPresentationOptionAlert);
    
    // Print Notification info
    NSLog(@"Userinfo %@",notification.request.content.userInfo);
}

如果你的应用程序处于前台状态,这意味着你正在使用同一个应用程序。所以通常没有必要在顶部显示通知。

但如果你想显示通知在那种情况下你必须创建你的自定义警报视图或自定义视图像Toast或其他东西来向用户显示你已经收到通知。

如果你的应用中有这样的功能,你也可以在顶部显示一个徽章。

对于任何可能感兴趣的人来说,我最终创建了一个自定义视图,看起来像顶部的系统推送横幅,但添加了一个关闭按钮(小蓝色X)和一个点击消息进行自定义操作的选项。它还支持在用户有时间阅读/删除旧通知之前到达多个通知的情况(没有限制可以堆积多少…)

链接到GitHub: AGPushNote

用法基本上是在线的:

[AGPushNoteView showWithNotificationMessage:@"John Doe sent you a message!"];

在iOS7上看起来是这样的(iOS6有一个iOS6的外观和感觉…)