2
votes

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);
        application.registerUserNotificationSettings(setting);
        application.registerForRemoteNotifications();

    }

following has been added:

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

and

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

and

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", 
   "sound":"default"
}

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

Thanks in advance.

Edit: 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 gateway.sandbox.push.apple.com 
2016-01-03 21:42:59 +0000: Set SSL connection 
2016-01-03 21:42:59 +0000: Set peer domain name gateway.sandbox.push.apple.com 
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 gateway.sandbox.push.apple.com:2195 
2016-01-03 21:42:59 +0000: Disconnected from server gateway.sandbox.push.apple.com:2195

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

1
Can you show the implementation of the methods you think are not working? didRegisterForRemoteNotificationsWithDeviceToken and didFailToRegisterForRemoteNotificationsWithError do not seem to be accurate, as they are lacking parameters and capitalization is not matching the declaration.Joride
for now i'am only doing print("notification recieved") in didReceiveRemoteNotification and ReceivedRemoteNotification in didRegisterForRemoteNotificationsWithDeviceToken I have to following: let deviceTokenStr = DeviceTokenToString(deviceToken) print(deviceTokenStr) where DeviceTokenToString is a little function that strips out < > and spacesMarc D.
Please include the full implementation in your question. Also, take a look at the console, not the debug one, but the device one. It might give you some errors that can help.Joride
console log line included in the questionMarc D.

1 Answers

1
votes

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.