1

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()
    }

}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Justin
  • 243
  • 5
  • 15
  • Are you receiving a token successfully? – Victor Sigler Jul 07 '15 at 22:01
  • I print the token and it looks alright to me. – Justin Jul 07 '15 at 22:02
  • 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 Jul 07 '15 at 22:19
  • The push notifications are being sent correctly. Just this function is not getting called for some reason – Justin Jul 08 '15 at 17:15
  • 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 Jul 08 '15 at 19:50
  • same problem. Any solution? – daniherculano Jul 27 '15 at 10:52

3 Answers3

3

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

ChrisMM
  • 8,448
  • 13
  • 29
  • 48
  • great answer, the problem I had is that with ```content_available:true``` it actually didn't work I used ```content_available: "true" ``` and now it works – StackGU May 26 '21 at 21:23
0

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

Vincenzo
  • 5,304
  • 5
  • 38
  • 96
0

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.

Prabhu E
  • 59
  • 4