1
votes

For some reason my didReceiveRemoteNotification is never called. I have done the following:

checklist of APNS:

Create AppId allowed with Push Notification

Create SSL certificate with valid certificate and app id

Create Provisioning profile with same certificate and make sure to add device

With Code:

Register app for push notification

Handle didRegisterForRemoteNotificationsWithDeviceToken method

Set targets> Capability> background modes> Remote Notification Handle didReceiveRemoteNotification

Yet my function does not seem to get called. My code looks like this:

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    if (application.respondsToSelector("isRegisteredForRemoteNotifications"))
    {
        // iOS 8 Notifications
        application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: (.Badge | .Sound | .Alert), categories: nil));
        application.registerForRemoteNotifications()
    }
    else
    {
        // iOS < 8 Notifications
        application.registerForRemoteNotificationTypes(.Badge | .Sound | .Alert)
    }


    return true
}


func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
    var tokenString = ""

    for var i = 0; i < deviceToken.length; i++ {
        tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
    }

    apnsID = tokenString
    println("******apnsID is \(apnsID)")
    dToken = deviceToken
    println("******dToken is \(dToken)")
    NSUserDefaults.standardUserDefaults().setObject(deviceToken, forKey: "deviceToken")
}

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

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {

    println("Getting notification")

    var message: NSString = ""
    var alert: AnyObject? = userInfo["aps"]

    println(userInfo["aps"])

    if((alert) != nil){
        var alert = UIAlertView()
        alert.title = "Title"
        alert.message = "Message"
        alert.addButtonWithTitle("OK")
        alert.show()
    }

}
3
Are you receiving a token successfully?Victor Sigler
I print the token and it looks alright to me.Justin
The tokens for the dev and prod environments are different, make sure you're not trying to send the push on the prod environment using the dev token or vice versa. Also you've only posted your iOS code and checklist, what about the server checklist and code? (Or are you using Parse?) Have you confirmed there are no errors sending the push?Gruntcakes
The push notifications are being sent correctly. Just this function is not getting called for some reasonJustin
The main reasons pushes don't get delivered is a) the dev/prod mismatch as I've already mentioned, b) the device token is incorrect / out of date. Are you trying to send a regular push or silent background push? If its a silent push try plugging your device into a power supply when you send the push.Gruntcakes

3 Answers

3
votes

add content_available:true in your request . for iOS payload data.You will get notification in didReceiveRemoteNotification. Now handle it.

0
votes

As I found out having the same problem myself, in order for didReceiveRemoteNotificationto be called, the incoming notification music carry an "available_content" : true parameter with the notification payload. So in case of you sending the notification in a dictionary form , as was my case in device to device pushes, you just have to add it in the dictionary as you would specify other parameters as you would for sound or badge, also needed if you're using some sort of push service as Postman

0
votes

Three steps:

  1. Add logic here to handle incoming notification:

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

completionHandler(.newData)

}

  1. Add Background mode to capabilities in target, and ensure to check 'Remote notifications'

  2. While sending a push notification, add "content_available" : true

This worked for me in iOS 14/Xcode 12.3.