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
let deviceTokenStr = DeviceTokenToString(deviceToken)
print(deviceTokenStr)
where DeviceTokenToString is a little function that strips out < > and spaces – Marc D.