在iOS应用中显示通知当应用程序在前景/活动在swift 3

时间:2017-05-23 07:11:28

标签: ios swift push-notification apple-push-notifications

我正在尝试在应用处于活动状态时显示横幅通知。 我使用了给定的方法,但没有结果,横幅通知仅在应用关闭时出现:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
    print("Push notification received: \(userInfo)")

    if application.applicationState == .active {
       print("active")
       let localNotification = UILocalNotification()
       localNotification.userInfo = userInfo
       localNotification.alertAction = "Test"
       localNotification.soundName = UILocalNotificationDefaultSoundName
       localNotification.alertBody = "Notification test!!"
       localNotification.fireDate = Date()
       UIApplication.shared.scheduleLocalNotification(localNotification)
     }
}

打印“有效”但未显示通知。我错过了任何一步吗?

谢谢。

3 个答案:

答案 0 :(得分:6)

如果应用程序在前台运行,iOS将不会显示通知横幅/警报。您必须编写一些代码来处理应用程序在前台接收通知时的情况。

或者您可以使用受欢迎的第三方库:github.com/bryx-inc/BRYXBanner

使用如下

import BRYXBanner // import in your class 

// Put this code where you are getting notification 
let banner = Banner(title: "title", subtitle: "subtitle", image: UIImage(named: "addContact"), backgroundColor: UIColor(red:137.0/255.0, green:172.0/255.0, blue:2.0/255.0, alpha:1.000))
banner.dismissesOnTap = true
banner.show(duration: 1.0)

但是如果您正在使用iOS 10.0+,那么您可以在app处于前台时接近显示横幅消息的目标,请使用以下方法。

 // This method will be called when app received push notifications in foreground for iOS 10+
 @available(iOS 10.0, *)
 func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    completionHandler([.alert, .badge, .sound])
 }

答案 1 :(得分:1)

在前台显示通知。你需要编写委托方法\

 @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter,  willPresent notification: UNNotification, withCompletionHandler   completionHandler: @escaping (_ options:   UNNotificationPresentationOptions) -> Void) {

        //Called when a notification is delivered to a foreground app.

        let userInfo = notification.request.content.userInfo as? NSDictionary
        print("\(userInfo)")

     }

    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

        // Called to let your app know which action was selected by the user for a given notification.
        let userInfo = response.notification.request.content.userInfo as? NSDictionary
        print("\(userInfo)")
    }

答案 2 :(得分:0)

添加此代码。您需要解析userInfo

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    let userInfo = notification.request.content.userInfo
    let notification = JSON(userInfo)
    print(notification)
}