简体   繁体   中英

didReceiveRemoteNotification not called Swift

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

}

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

As I found out having the same problem myself, in order for didReceiveRemoteNotification to 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM