PushNotification横幅未在ios 10.3中显示

时间:2017-11-14 06:06:39

标签: ios swift swift3 push-notification

我已将推送通知集成到我的项目中,出于某种原因,当我处于背景情绪中而不是当我在前台时,横幅会显示。我确定我错过了什么,我的代码如下:任何帮助都会非常感激。

import UserNotifications
class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate {

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        let center  = UNUserNotificationCenter.current()
        center.delegate = self //not sure this is appropriate 
        registerForPushNotifications()
}
func registerForPushNotifications() {
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
            (granted, error) in

            guard granted else { return }
            self.getNotificationSettings()
        }
    }
    func getNotificationSettings() {
        UNUserNotificationCenter.current().getNotificationSettings { (settings) in
            print("Notification settings: \(settings)")
            guard settings.authorizationStatus == .authorized else { return }
            DispatchQueue.main.async(execute: {
            UIApplication.shared.registerForRemoteNotifications()
            })
        }
    }
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

        let tokenParts = deviceToken.map { data -> String in
            return String(format: "%02.2hhx", data)
        }

        let token = tokenParts.joined()
        if let valueInDB = UserDefaults.standard.value(forKey: "Permission_Granted") {

        UserDefaults.standard.setValue(token, forKey: "PUSH_TOKEN")
        UserDefaults.standard.synchronize()
        print("Device Push Token: \(token)")


    }

每次收到通知时都会调用它,即使它在前台或后台运行。因此批处理图标会更新。

func application(_ application: UIApplication,
                         didReceiveRemoteNotification userInfo: [AnyHashable : Any],
                         fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
        {

            print("Recived: \(userInfo)")

            completionHandler(.newData)

            self.onPushRecieved = MP3ViewController().catchPushNotifications
            let notification = JSON(userInfo)
            print(notification)

            let mediaId = (notification["media_id"]).stringValue
            print(mediaId)
            var badgeCount = 0
            var pushAvailable = false


            if let pushCount = UserDefaults.standard.value(forKey: "PUSH_COUNT"){
                badgeCount = pushCount as! Int
                print(badgeCount)
                badgeCount = badgeCount + 1
                UIApplication.shared.applicationIconBadgeNumber = badgeCount
            }

            print(badgeCount)
            UserDefaults.standard.setValue(badgeCount, forKey: "PUSH_COUNT")
            UserDefaults.standard.synchronize()

            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            print(isOnBackGround)
            if isOnBackGround {
                if mediaId != nil{
                    DispatchQueue.main.async {
                        let mP3ViewController = storyboard.instantiateViewController(withIdentifier:"MP3ViewController") as! MP3ViewController
                        mP3ViewController.media_ID = mediaId
                        self.navigationController?.pushViewController(mP3ViewController, animated:true)
                        self.isOnBackGround = false

                    }

                }
            }else{
                print("Running on foreground")

            }

        }
    }

1 个答案:

答案 0 :(得分:2)

您可能想要实现以下功能,例如:

public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) {
    completionHandler([.badge, .alert, .sound])
}
该代表的

UNUserNotificationCenterDelegate