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

5

30

I have several apps that were written in Swift 2.2, compiled with Xcode 7.3 and is live on the App Store. The apps utilize Push Notifications and is working fine in iOS 9.3 and earlier.

On devices that have been upgraded to iOS 10, however, my apps don't receive any Push Notifications. Devices that are still running iOS 9 are still receiving notifications.

Thinking it might be a certificate or entitlement issue, I have tried the following: Upgraded one of my apps to Swift 2.3, added the APS Environment Entitlement and compiled it in Xcode 8 but this made no difference.

In my AppDelegate I still have the existing methods to register for push notifications that includes:

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

This registration seems to be successful even on iOS 10 since Application didRegisterForRemoteNotificationsWithDeviceToken is then called, so I am receiving a token from APNS.

The problem is that when I send a push notification to this device, Application didReceiveRemoteNotification is never called.

Now, here it is said that the methods on UIApplicationDelegate is deprecated on iOS 10 and I should implement userNotificationCenter(:didReceive:withCompletionHandler:) and userNotificationCenter(:willPresent:withCompletionHandler:)

The problem is that I am not ONLY targeting iOS 10. I still need the app to work on iOS 8 and 9 so I doubt that it's the correct approach to implement those methods.

How do I get push notifications for an existing app to continue working on devices that have been upgraded to iOS 10? Do I need to rewrite code? Do I just need to update some certificates or entitlements and recompile in Xcode 8 with some "new" settings?

Inflect answered 16/9, 2016 at 13:38 Comment(12)
there are several pre existing ans for this "push notification not works in iOS 10" ..Assurgent
Could you please refer to the answer you're referring to vaibhav? I have searched for two days now and could not find any questions or answers applicable to the scenario I describe.Inflect
sure link 1, link 2 ..Assurgent
Is there perhaps a Swift answer you could refer to?Inflect
You can follow this link its in Objective C but you can get some idea #39573397Become
@Stanley, did you find a answer?Swanson
@ilanbt No, I am still searching and trying some alternatives... :-(Inflect
@Stanley, I figured it out, see my answer.Swanson
@ilanbt Me too :-) We just posted seconds apart I thinkInflect
Check Url for the solution i found my solution here.Fomalhaut
Possible duplicate of Registering for Push Notifications in Xcode 8/Swift 3.0?Wendiewendin
Have you found a solution ? I just opened a question here about: #41639343Logarithmic
I
52

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.

Inflect answered 21/9, 2016 at 13:38 Comment(7)
VERY IMPORTANT: We are using our own push notification server for messages. Besides the client changes I describe here, we also had to make server changes to some of our silent notifications. We had some silent notifications that had the structure {"my_field":"some_value", "my_field2":"myvalue2"} that worked fine before iOS 10, but it seems like on iOS 10 one has to include either an "alert" or "aps" field as well. So I had to change my message on server to be {"my_field":"some_value", "my_field2":"myvalue2", "aps":{"content-available":1}} otherwise I don't receive anything from apns.Inflect
You are officially my heroWainscot
Thatssss very useful answer. Thank u StanleyRevelry
Error: No such module 'UserNotifications'Kaneshakang
Except that you DON'T WANT TO REQUEST PERMISSIONS ON APP START!Daune
You implement those 2 new methods for iOS 10 to handle the user's action to the notifications! "The method will be called on the delegate when the user responded to the notification by opening the application, dismissing the notification or choosing a UNNotificationAction". Downvoted because it's spreading false information.Daune
@Stanley.. I am facing issue with push notifications since couple of weeks #41199151 can receive data message from server,But i could not able to receive notification from server.Koa
S
9

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)
}
Swanson answered 21/9, 2016 at 13:20 Comment(1)
This is swift 3.0 syntax.Revelry
C
8

I have fixed this issue using below step:

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

enter image description here Step 2: Fix issue related to proper certificate,provisioning profile

Step 3: Quit Xcode, Clean and Run

Capful answered 23/11, 2016 at 5:53 Comment(1)
this answer is sweet for me. In my case, iOS9.x did received device token but iOS10 did notKenogenesis
C
2

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

Copilot answered 23/9, 2016 at 18:29 Comment(1)
Can confirm - just started implementing the accepted answer by adding Entitlements file and it worked without any additional changes on iOS 10 :)Diversion
I
0

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.

Inquisitor answered 1/2, 2017 at 18:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.