Marc D. Marc D. - 1 year ago 72
Swift Question

didReceiveRemoteNotification or ReceivedRemoteNotification never fires

For some reason didReceiveRemoteNotification or ReceivedRemoteNotification never fires; when I implemented it a while ago, it was just fine, at least when I was testing it for when the app was in the background. Now 2 weeks later, and my app is more mature, I was getting back at the push notification part, only to find out that at no particular moment the events are being fired. The device is receiving the push notification popup, and when I click it, the app opens, but didFinishLaunchingWithOptions launchOptions is nil.

What could have caused this to stop working?

  • Push notifications enabled

  • Background Modes (remote notifications and Background fetch) enabled

in didFinishLaunchingWithOptions:

if application.respondsToSelector("registerUserNotificationSettings:") {

let setting = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil);


following has been added:

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
deviceTokenStr = DeviceTokenToString(deviceToken)


func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
print("Device token for push notifications: FAIL")

then the two following func:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print("notification received")


func application(application: UIApplication, ReceivedRemoteNotification userInfo: [NSObject : AnyObject]) {
print("notification received")

didRegisterForRemoteNotificationsWithDeviceToken is returning the token for push, and in the simulator it invokes didFailToRegisterForRemoteNotificationsWithError , so far all runs fine.

When the app is in the background, and instruct the server to send a push, it does arrive on the phone, and click on it, will open the app.

but nothing inside didReceiveRemoteNotification or ReceivedRemoteNotification gets ever fired.

I have looked at numerous stackoverflow posts, but none of them seem to be having the same problem, and or solution, after trying all kinds of combinations in different posts, I can't seem to get it back to work.

apn payload is basic just "aps":

"alert":"string message",

Would be great if somebody could give me a hand, to find out why it stopped.

Thanks in advance.

added content to the functions

When app is running, it will only show the 64 characters of the push token in the log, no errors are shown.

on the device console log it shows this on time the push notification arrives and when the app is in the background:

Jan 3 21:20:29 Marcs-iPhone SpringBoard[58] <Warning>: SBLockScreenNotificationListController: Attempting to remove a bulletin I don't have

nothing happens in the console log when the app is the active app.

Edit 2:
Log added from APN Tester

2016-01-03 21:42:59 +0000: Connected to server
2016-01-03 21:42:59 +0000: Set SSL connection
2016-01-03 21:42:59 +0000: Set peer domain name
2016-01-03 21:42:59 +0000: Keychain Opened
2016-01-03 21:42:59 +0000: Certificate data for Apple Push Services: com.xxxxxxx.xxxxxxxx initialized successfully
2016-01-03 21:42:59 +0000: Sec Identity created
2016-01-03 21:42:59 +0000: Client certificate created
2016-01-03 21:42:59 +0000: Connected
2016-01-03 21:42:59 +0000: Token: <xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx>
2016-01-03 21:42:59 +0000: Written 92 bytes sending data to
2016-01-03 21:42:59 +0000: Disconnected from server

The message did arrive on the phone when executing the apn sent, albeit nothing showed up in device console log, nor debug output

Answer Source

Sorted it out, and will answer here in case someone else runs into the same problem.

After some long digging around, the code is functional and working, I had one workaround in my project to clear out the _handleNonLaunchSpecificActions:forScene exception problem.

extension UIApplication {
    func _handleNonLaunchSpecificActions(arg1: AnyObject, forScene arg2: AnyObject, withTransitionContext arg3: AnyObject, completion completionHandler: () -> Void) {
        //catching handleNonLaunchSpecificActions:forScene exception on app close

after removing this workaround, the notifications are working like a charm again.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download