Push Notifications not being received on iOS 10, but working on iOS 9 and before

删除回忆录丶 提交于 2019-11-27 11:22:16

Ok I have figured it out. I now have my original Push Notifications that was working on iOS 9 working on iOS 10 with Xcode 8 and Swift 2.3.

I implemented this by making the following changes to my AppDelegate:

1) On my project settings, under the Capabilities Tab, I scroll down to "Push Notifications" and turn it to "ON". This automatically generates an entitlements file that contains the key "APS Environment" and with value "development".

2) In AppDelegate.swift I make several code changes. I start by importing the UserNotifications framework:

import UserNotifications

Then I have AppDelegate implement the UNUserNotificationCenterDelegate protocol:

class AppDelegate: /*Some other protocols I am extending...*/, UNUserNotificationCenterDelegate {

Then I add the following method:

func registerForPushNotifications(application: UIApplication) {

    if #available(iOS 10.0, *){
        UNUserNotificationCenter.currentNotificationCenter().delegate = self
        UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert], completionHandler: {(granted, error) in
            if (granted)
            {
                UIApplication.sharedApplication().registerForRemoteNotifications()
            }
            else{
                //Do stuff if unsuccessful...
            }
        })
    }

    else{ //If user is not on iOS 10 use the old methods we've been using
        let notificationSettings = UIUserNotificationSettings(
            forTypes: [.Badge, .Sound, .Alert], categories: nil)
        application.registerUserNotificationSettings(notificationSettings)

    }

}

Then I call this newly created function inside didFinishLaunchingWithOptions:

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

I leave my didRegisterForRemoteNotificationsWithDeviceToken method unchanged:

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    //Implement this. Do something with the token I receive. Send it to my push notification server to register the device or something else I'd like to do with the token.
 }

Now I implement two new methods for iOS 10 to handle the receiving of Push Notifications:

@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
    //Handle the notification
}

@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
    //Handle the notification
}

I did not remove any of the methods I have previously implemented for Push Notifications in iOS 9.

ilan

IOS 10 has a different Notification kit.

import UserNotifications

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



    if #available(iOS 10.0, *) {
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options: [.badge, .alert , .sound]) { (granted, error) in
            if granted {
                UIApplication.shared.registerForRemoteNotifications()
            }
        }
    } else {
        let type: UIUserNotificationType = [UIUserNotificationType.badge, UIUserNotificationType.alert, UIUserNotificationType.sound]
        let setting = UIUserNotificationSettings(types: type, categories: nil)
        UIApplication.shared.registerUserNotificationSettings(setting)
        UIApplication.shared.registerForRemoteNotifications()
    }
}



func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data){
    let token = String(format: "%@", deviceToken as CVarArg)
}

I have fixed this issue using below step:

Step 1:Go to Project -> Target -> Capabilities -> Push Notifications -> Switch it ON

Step 2: Fix issue related to proper certificate,provisioning profile

Step 3: Quit Xcode, Clean and Run

In various SO responses to questions about iOS 9 vs iOS 10 push notifications, I've tried the following tests:

1) update the notification calls to specifically use the UNUserNotificationCenter for iOS 10 devices

2) update the Push capability so that the entitlements file is updated

I can confirm that doing #2 and keeping all your existing iOS 9 push code the same will allow basic push notifications to come through on the iOS 10 devices

When my app was moved from IOS 9 to IOS 10 the silent push notifications sent by my Parse server hosted on Heroku stopped being received by the app.

The only change I made to make notifications work again was to modify the Javascript on my server so that the argument to content-available was an integer 1 and not a string "1"

           Parse.Push.send({
                where: notifyDeletedRequestsQuery,
                data: {
                    'content-available': 1,
                    refresh: constants.requestsClassName
                    }
            }, { useMasterKey: true });

Also note that content-available cannot appear in the same push notification as badge, sound or alert.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!