phonegap / phonegap-plugin-push

Register and receive push notifications
MIT License
1.94k stars 1.91k forks source link

didReceiveRemoteNotification not triggered when app in background #1132

Closed presidenten closed 7 years ago

presidenten commented 7 years ago

Expected Behaviour

When the application is running in the background and receives a notification with 'content-available' = 1, the didReceiveRemoteNotification should run and trigger my on('onNotification') function in javascript.

Actual Behaviour

I receive a notification that is displayed, but nothing happens on the iphone. If I press the notification the app is started and the notification is processed. If I switch to the app, the notification is lost in cyberspace.

Reproduce Scenario (including but not limited to)

Steps to Reproduce

cordova platform add ios cordova add plugin through "cordova plugin add phonegap-plugin-push --variable SENDER_ID="my id" cordova build ios

send var data = { title: 'A notification', 'content-available': '1' }; via node-pushnotifications (which uses node-apn) with the app open and look at log to see javascript being run. Put app in background and send notification again. Receive notification and see no javascript being run and also notice that the log from didReceiveRemoteNotification is not printed in the log. #### Platform and Version (eg. Android 5.0 or iOS 9.2.1) Ios: 9.3.3 #### (Android) What device vendor (e.g. Samsung, HTC, Sony...) #### Cordova CLI version and cordova platform version Cordova CLI: 6.3.0 Gulp version: CLI version 3.9.0 Gulp local: Local version 3.9.1 Ionic Framework Version: 2.0.0-beta.8 Ionic CLI Version: 2.0.0-beta.33 Ionic App Lib Version: 2.0.0-beta.19 ios-deploy version: 1.8.6 ios-sim version: 5.0.8 OS: Mac OS X El Capitan Node Version: v6.2.2 Xcode version: Xcode 7.3.1 Build version 7D1014 #### Plugin version phonegap-plugin-push 1.8.0 "PushPlugin" #### Sample Push Data Payload var data = { title: 'A notification', 'content-available': '1' }; #### Sample Code that illustrates the problem #### Logs taken while reproducing problem When its working: 2016-07-30 17:04:34.432 PokeNotifier for Pokemon GO[3285:2089374] didReceiveNotification with fetchCompletionHandler 2016-07-30 17:04:34.432 PokeNotifier for Pokemon GO[3285:2089374] app active 2016-07-30 17:04:34.433 PokeNotifier for Pokemon GO[3285:2089374] Notification received 2016-07-30 17:04:34.433 PokeNotifier for Pokemon GO[3285:2089374] Push Plugin key: alert 2016-07-30 17:04:34.433 PokeNotifier for Pokemon GO[3285:2089374] Push Plugin key: badge 2016-07-30 17:04:34.433 PokeNotifier for Pokemon GO[3285:2089374] Push Plugin key: sound 2016-07-30 17:04:34.449 PokeNotifier for Pokemon GO[3285:2089374] got notification!! 2016-07-30 17:04:34.450 PokeNotifier for Pokemon GO[3285:2089374] { "additionalData": { "content-available": "1", "foreground": true, "coldstart": false }, "message": "A wild boar appeared!\nClick for coordinates!!", "count": 1, "sound": "ping.aiff" } When its not working: (nothing)
macdonst commented 7 years ago

@presidenten try without the quotes around 1. IIRC iOS needs it to be an int.

var data = {
title: 'A notification',
'content-available': 1
};
presidenten commented 7 years ago

Thanks, I just tried, and still no response. Thought I might also say that I have "Remote notifications" on in "Background modes" in the project. I also double checked that I have given all permissions on my iphone.

jcesarmobile commented 7 years ago

If you send content-available = 1 that means it's a silent notification:

For a silent notification, take care to ensure there is no alert, sound, or badge payload in the aps dictionary. If you don’t follow this guidance, the incorrectly-configured notification might be throttled and not delivered to the app in the background, and instead of being silent is displayed to the user.

https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html

presidenten commented 7 years ago

@jcesarmobile Just tried sending this (print out from node-pushnotifications on npm (uses node-apn))

{ encoding: 'utf8', payload: { 'content-available': 1 }, expiry: 1469895173, priority: 10, retryLimit: -1, device: undefined, compiled: false, truncateAtWordEnd: false, content_available: true }

Still nothing. This time ofcourse not even a notification.

I receive it when the app is in the foreground though, then it looks like this (from javascript) 2016-07-30 17:56:54.103 PokeNotifier for Pokemon GO[3327:2108881] got notification!! 2016-07-30 17:56:54.103 PokeNotifier for Pokemon GO[3327:2108881] { "additionalData": { "content-available": 1, "foreground": true, "coldstart": false } }

presidenten commented 7 years ago

Woho! I got it to work!!!

Very strange though. I had to set contentAvailable: 1 camelcased, not snakecased, together with the defaultData in the node plugin settings.

Then I got the following in app:

{
    "content-available": 1,
    "additionalData": {
        "foreground": false,
        "coldstart": false
    }
}

Where the content-available is outside the additionalData.

Interestingly enough I also get it to work together with a notification text now. Once again, thanks guys! It felt that I tried everything, but then I finally tried to print out the contentAvailable variable in AppDelegate+notification.m and saw it was 0 all the time no matter and then just took a swing at trying camelcased :-)

Maybe its the node push plugin that does something strange? I dont know. I dont care. Now it works. Im happy :-)

Im closing this and leaving it for future reference.

lock[bot] commented 6 years ago

This thread has been automatically locked.