推送通知未在iOS 10设备中显示

时间:2017-07-20 04:40:10

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

我是新手的Swift和推送通知没有在iOS 10中弹出,我做了所有配置的事情。 iOS 8和9设备中的通知显示(相同代码)但未在iOS 10设备中显示。代码就像(只添加通知部分代码)。

private let brandFullery   : String = "some token key"

    var pushNotify : NSDictionary!

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        UIApplication.sharedApplication().applicationIconBadgeNumber = 0
        UIApplication.sharedApplication().cancelAllLocalNotifications()

        MyCacheManager.sharedInstance.deviceId = UIDevice.currentDevice().identifierForVendor!.UUIDString



        application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil))
        application.registerForRemoteNotifications()

        MyCacheManager.sharedInstance.deleteAllFileInDocument("LiveUserProfile")

        if let payload = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary, metData = payload["data"] as? NSDictionary {

            pushNotify = metData

            let aps = pushNotify["eventType"] as! NSString
            if aps != "INVITE_POINTS" && aps != "UPDATE_BEACON" && aps != "CREATE_REWARD" {

                MyCacheManager.sharedInstance.isFromNotification = aps as String
            } else {
                if aps == "UPDATE_BEACON" {

                    MyCacheManager.sharedInstance.isBeaconUpdate = true 
                }

                MyCacheManager.sharedInstance.isFromNotification = ""
            }

            self.createMenuView()

        } else {

            MyCacheManager.sharedInstance.isFromNotification = ""
            self.createMenuView()
        }


        #if BRANDAPP

            Twitter.sharedInstance().startWithConsumerKey(brandTwitterAuthKey, consumerSecret: brandSecuriteKey)

            Flurry.startSession(brandFullery)
        #else
            Twitter.sharedInstance().startWithConsumerKey(userTwitterAuthKey, consumerSecret: userSecuriteKey)

            Flurry.startSession(userFullery)

        #endif

        Fabric.with([Twitter.self])

        UIApplication.sharedApplication().statusBarStyle = .LightContent

        return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
    }

//MARK: Push notification
    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

        var deviceTokenString : NSString = deviceToken.description;
        deviceTokenString = deviceTokenString.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "<>"))
        deviceTokenString = deviceTokenString.stringByReplacingOccurrencesOfString(" ", withString: "")

        MyCacheManager.sharedInstance.deviceToken = deviceTokenString as String
        MyCacheManager.sharedInstance.putDeviceToken(deviceTokenString)

         NSLog("device %@", deviceTokenString)
    }


    func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    }

    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {


        MyCacheManager.sharedInstance.isFromNotification = "MSG"
        print(userInfo)
    }

请有人帮我解决这个问题。非常感谢提前。

2 个答案:

答案 0 :(得分:1)

您必须注册ios 10.试试这个

   import UserNotifications

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

 // iOS 10+ support
            if #available(iOS 10.0, *) {
                let center  = UNUserNotificationCenter.current()
                center.delegate = self
                center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
                    if error == nil{
                        UIApplication.shared.registerForRemoteNotifications()
                    }
                }

            }
                // iOS 9 support or less
            else {
                UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
                UIApplication.shared.registerForRemoteNotifications()
            }
}

// Called when APNs failed to register the device for push notifications

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

   let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
   print("APNs device token: \(deviceTokenString)")

}

然后你会收到

的通知
   // Receive displayed notifications for iOS 10 devices.
    //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) {
        let userInfo = notification.request.content.userInfo
        print("User Info = ",userInfo)

        completionHandler([.alert, .badge, .sound])
    }

    //MARK:- Handle push notifications
    //Called to let your app know which action was selected by the user for a given notification.
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

        let userInfo = response.notification.request.content.userInfo

        print("User Info = ",userInfo)
        //handle the notification form here when cliked.
        completionHandler()
    }

答案 1 :(得分:0)

简单形式

if #available(iOS 10, *) {
        UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in }
        application.registerForRemoteNotifications()
    }
    else {
        UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
        UIApplication.shared.registerForRemoteNotifications()
    }

然后有来自IOS 10的新代表

func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {

}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    //here we will get the device token
}