Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

notification event not called on ios #414

Closed
yoav-zibin opened this issue Dec 11, 2015 · 18 comments
Closed

notification event not called on ios #414

yoav-zibin opened this issue Dec 11, 2015 · 18 comments
Labels
Milestone

Comments

@yoav-zibin
Copy link

Hi,
First, thanks for the awesome plugin!
My issue is that the notification event is not getting called on ios (but it works perfectly fine on android).

The exact scenario:
I see the push notification, and click on it. This opens the app, but notification event is not getting called on iOS.

Do I need to call
push.on('notification', ...
immediately when the app starts, even before I get the deviceready event?
Because, in my app, I fetch the phonegap JavaScript code from the internet, so I call
push.on('notification', ...
only a couple of milliseconds after deviceready. So I'm afraid that maybe I "missed" the notification.

I don't understand though why I don't "miss" the notification on Android...

I'm not using background notifications.

@fredgalvao
Copy link
Collaborator

Why are you fetching the plugin's code from the internet, since they are already available locally from cordova?

@yoav-zibin
Copy link
Author

Thanks for the quick reply :)
So, is it a requirement to setup the event handlers immediately (or you might miss the notification)? There is no caching of the notifications until the handlers are set, right?

I'm fetching my event handlers code from the internet so it will always be up-to-date and so I could fix bugs without rebuilding the APK.
It's a similar concept to phonegap-build hydration (http://docs.build.phonegap.com/en_US/tools_hydration.md.html), but because I don't have control over the apps, I needed my own mechanism.

I'm using it in my multiplayer gaming platform:
https://github.com/yoav-zibin/phonegap-tictactoe/blob/gh-pages/www/index.html
(http://goo.gl/G8QNcQ)

I think I'll solve it by also saving some code in localStorage, to avoid "missing" notifications:
try {
var savedCode = localStorage.get("savedCode");
eval(savedCode);
} catch (e) {
console.error(e);
}

It's just weird it's happening only on iOS (and not on Android). But maybe Android is slower to fetch the notifications, so I never saw it happening on android.

@fredgalvao
Copy link
Collaborator

I mean, I can understand if you're getting the following code from outside:

var push = PushNotification.init(...)
push.on('registration', ...);
...
...
...

But by "I fetch the phonegap JavaScript code from the internet" I inderstood that you're actually fetching the plugin's code from the internet too, and that's not good (if that's indeed the case).

You don't need to set up your handlers immediately. As soon as you set them up, if a cached message is present, it'll be sent to your handlers no matter when you register them. The only soft requirement is that handlers are registered in the same CPU cycle that you call .init().

//this is okay
var push = PushNotification.init({...});
push.on('notification', function(){...});

//this is not
var push = PushNotification.init({...});
setTimeout(function() {
    push.on('notification', function(){...});
});

@yoav-zibin
Copy link
Author

Thanks, this is saving me a lot of time :)

Yes, I'm doing it in the same CPU cycle. Happy to know the notification is cached until I call init :)

I've done some more experiments and the problem is only on iOS when the app is closed.
(if the app is open or in the background, then it's working as expected:

  • if the app is open, then the notification handler is called without seeing any notification.
  • if the app is in the background, then when I click on the notification then my handler is called.)

That's why I still suspect it's either:

  1. some sort of race condition (maybe I'm calling "init" too late?)
  2. maybe it's a bug in the plugin --- was this use-case tested on iOS? (making sure the notification handler is called after clicking on a notification for an app that is closed, i.e., coldstart=true. On android)
  3. maybe I need to handle background notification in iOS to support that use-case? I didn't need to do that for android :)

I use this version:
<gap:plugin name="phonegap-plugin-push" source="npm" version="1.2.3" />

BTW, on Android, the use-case I'm talking about is when:
"coldstart":true,"foreground":false
E.g., when the app is closed, and I click on a notification I get:
Time 1.044, LOG: Process message from phonegap:,{"addLog":["PushNotification notification:",{"title":"You lost","message":"Yoab Zibin won","additionalData":{"updatedTimestampMillis":"1449954784551","from":"24803504516","notId":"3249660004697307829","notificationOpponentId":"5132411864088576","gcm.notification.icon":"new","matchId":"3249661450088402834","coldstart":true,"collapse_key":"do_not_collapse","foreground":false}}]}

BTW, after looking at your code I realized that notId is an INTEGER (not LONG):
int notId = parseInt(NOT_ID, extras);
This should be documented, because I used "System.currentTimeMillis()" to generate the notId :)

@yoav-zibin
Copy link
Author

The version I used got truncated/removed because github removed html elements :)
The version I use is phonegap build:
gap:plugin name="phonegap-plugin-push" source="npm" version="1.2.3"

@yoav-zibin
Copy link
Author

BTW, I changed the notId to be an int, but the issue still happens on iOS.
On Android, the fact that it was a long caused an issue with collapsing notifications because:
java.lang.NumberFormatException: For input string: "3249660004697307829"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:495)

I summarized the problem below.
There are 3 types of notifications: coldstart, background, and foreground.
My problem is with coldstart on iOS: the notification handler is not called.
I collected an example on iOS&Android for each notification type (except coldstart on iOS, which doesn't work :))

Full example for foreground message:
Android:
{"title":"Your turn",
"message":"Yoab Zibin just played",
"additionalData":{
"updatedTimestampMillis":"1449962800733",
"from":"24803504516","notId":"287630958",
"notificationOpponentId":"5132411864088576","gcm.notification.icon":"new",
"matchId":"5499075749368249052","collapse_key":"do_not_collapse","foreground":true}}
iOS:
{"count":6,
"message":"New game\nYoav Zibin invited you to play",
"additionalData":{
"notificationOpponentId":"4791088564928512",
"foreground":true,
"matchId":"5499075749368249052",
"message":"Yoav Zibin invited you to play",
"notId":"287330068",
"updatedTimestampMillis":"1449962516259",
"title":"New game"}}

Full example for background message:
Android:
{"title":"New game","message":"Yoab Zibin invited you to play",
"additionalData":{"
updatedTimestampMillis":"1449964270615","from":"24803504516",
"notId":"515844556","notificationOpponentId":"5132411864088576",
"gcm.notification.icon":"new","matchId":"5242552702841088666",
"coldstart":false,
"collapse_key":"do_not_collapse",
"foreground":false}}
iOS:
{"count":2,
"message":"Your turn\nYoav Zibin just played"
"additionalData":{
"notificationOpponentId":"4791088564928512",
"foreground":false,
"matchId":"1507619365451967884",
"message":"Yoav Zibin just played",
"notId":"1296834422",
"updatedTimestampMillis":"1449955979140",
"title":"Your turn"}
}

Full example for coldstart message:
Android:
{"title":"You lost","message":"Yoab Zibin won",
"additionalData":{
"updatedTimestampMillis":"1449954784551",
"from":"24803504516","notId":"4697307829",
"notificationOpponentId":"4088576",
"gcm.notification.icon":"new",
"matchId":"3249661450088402834",
"coldstart":true,"collapse_key":"do_not_collapse",
"foreground":false}}
iOS:
Can't get a coldstart message on iOS! See this bug :)

Note that coldstart field is completely missing from iOS (for the case of background message, on Android, I have "coldstart":false, but that doesn't exist on iOS).
So I have a suspicion that iOS doesn't handle coldstart?

@yoav-zibin
Copy link
Author

Yes, more than a suspicion --- iOS doesn't handle coldstart (I love open source --- I can just search the code :))
coldstart is never mentioned in objective-c code (only android Java code).

How am I supposed to handle coldstart?
Do I have to do background handling of messages on iOS? (https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#background-notifications-1)

@fredgalvao
Copy link
Collaborator

So, you're using 1.2.3, and afaik that version didn't yet have iOS coldstart notification support. I may be wrong, and I'm coming to realize we still need to document these scenarios better.

Phonegap Build now supports gradle builds, so could you try and upgrade your plugin to the latest one and retest?

@yoav-zibin
Copy link
Author

But I searched the code and I only see "coldstart" in Android code:
https://github.com/phonegap/phonegap-plugin-push/search?utf8=%E2%9C%93&q=coldstart
Are you sure iOS supports now coldstart notifications?

@yoav-zibin
Copy link
Author

Emm, I have problems upgrading to version 1.3. Do you have an example of config.xml that works on phonegap build?

I removed all other plugins, I updated the plugin to 1.3, and I even set:

(I started with 7, then increased to 14, then to 19)
See:
https://github.com/yoav-zibin/phonegap-tictactoe/blob/gh-pages/www/config.xml

The error I get:
Error - One of your plugins required the gradle android build tool to build - You can fix this here

In the build logs:
Build Date: 2015-12-13 11:57:33 +0000
/project/cordova/node_modules/q/q.js:126
throw e;
^

Error: Project contains at least one plugin that requires a system library. This is not supported with ANT. Please build using gradle.
at /project/cordova/lib/build.js:183:27
at _fulfilled (/project/cordova/node_modules/q/q.js:798:54)
at self.promiseDispatch.done (/project/cordova/node_modules/q/q.js:827:30)
at Promise.promise.promiseDispatch (/project/cordova/node_modules/q/q.js:760:13)
at /project/cordova/node_modules/q/q.js:574:44
at flush (/project/cordova/node_modules/q/q.js:108:17)
at doNTCallback0 (node.js:417:9)
at process._tickCallback (node.js:346:13)

In phonegap build docs, they only mention minSdkVersion and grade:
Invalid minSdkVersion
As PhoneGap 4.x moves to Gradle builds minSdkVersion will now be enforced. This means the minSdkVersion of your application, as set in your config.xml or the default (14 for phonegap 4.x) must be greater than any Libraries included in the application. Typically these libraries will be part of a plugin. To find out the minSdkVersion for a particular plugin it is typically in the AndroidManifest.xml file. The value can also be seen in the error message contained in the build log file of the failed build.
To fix this error add a preference being sure the value is equal to or higher than the minSdkVersion specified in any included plugins.

@fredgalvao
Copy link
Collaborator

Please add this to your config.xml to enable gradle builds on PGB:

<preference name="android-build-tool" value="gradle" />

@macdonst macdonst added this to the Release 1.5.0 milestone Dec 13, 2015
@yoav-zibin
Copy link
Author

Ok, upgraded successfully to latest version:
phonegap-plugin-push npm * n/a 1.4.5 android,ios,winphone

But the notification handler is no longer called :(
Did the JS API changed between 1.2.3 and 1.4.5 ?
I look at the docs and it seems to be the same:
push.on('notification', ...

I tried it twice (7:50 and 7:59):
On my android I confirm that I consistently get the same registration ID:

PushNotification registration: Object device token = fQ3Dg8_jcmM:APA91bEFChZPmKPOD9wjWzj8kxGQ75AVyfYZ0rs4EIt51fByS4yH65XcI5hCrSqjflcLzlXO7y_FAWXX3MdbNvM3acLLG3LZ6YcstTGA2VyWMnXGm6bWtmrKp3JvACYnGUfhy6H2hAUM platformType= ANDROID

On the server side I see that it was sent:
2015-12-14 07:50:07.491
com.multiplayer.PostMessage sendPost: Sending POST message to url: https://gcm-http.googleapis.com/gcm/send
Message: {"registration_ids":["fQ3Dg8_jcmM:APA91bEFChZPmKPOD9wjWzj8kxGQ75AVyfYZ0rs4EIt51fByS4yH65XcI5hCrSqjflcLzlXO7y_FAWXX3MdbNvM3acLLG3LZ6YcstTGA2VyWMnXGm6bWtmrKp3JvACYnGUfhy6H2hAUM"],"data":{"title":"Your turn","message":"yoav local chrome just played","matchId":"3638701225606354202","notificationOpponentId":"5720693702393856","updatedTimestampMillis":"1450097407127","notId":"16866400","matchInfoJson":"{"gameVariant":0,"matchId":"3638701225606354202","createdTimestampMillis":"1450045072476","updatedTimestampMillis":"1450097407127","isReservedAutoMatch":false,"notificationOpponentId":"0","state":"N4IgLgrgTgdgkjAJgUwB4CFkDMD2VkCyOAbsiAFwAMANONPEmhQIy0C2JyAchGwEbIoFAMy0oAQyQ42AZWTJEFEJQB0AdmHCAnFoAcm3QCZDANmNaQtADbiAzmCKkKAbVC3kYACr0KoSLAQUVBYAXxDqNw9fEABrZABPJXtxMDJaYnErCDJyUBQrMHFoqBwAdwoaEABjHCsK8JA+HHEoRXJnZxAADUsQXoB5EABdak7e8ZGx2h7aYaGwkJGQG3sZQtTo5I3ckHzC4rKK2hq68kMGppa2jr7Z2kHJ26fh0aeZvqH5hqrofBgwNYpHJudbA3bIApFHYlcpUY61eq0S6tFydd4DF5TZ6PXroz5hax2MAANQAlrZSXwrMhPDhfN9fsh/mSKVSaXTcmEgA\u003d\u003d\u003d","lastPlayerIndex":0,"nextPlayerIndex":1,"playersInfo":[{"playerId":"5720693702393856","displayName":"yoav local chrome","avatarImageUrl":"http://www.multiplayer-gaming.com/v1/dist/imgs/avatar38.gif","isOnline":true},{"playerId":"4791088564928512","displayName":"Yoav Zibin","avatarImageUrl":"http://graph.facebook.com/10152824135331125/picture?square\u003dsquare","isOnline":true}]}"},"notification":{"title":"Your turn","body":"yoav local chrome just played","icon":"new"}}
I 2015-12-14 07:50:07.541
com.multiplayer.PostMessage sendPost: POST returned: {"multicast_id":4962020824450216528,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1450097407513102%224af683224af683"}]}

On Google Play, the notifications now look different:
pushnotificationbug2

Any idea why?

Again, I created a bug report (like I did for #419), but I don't want to attach it publicly. Happy to email it :)

@yoav-zibin
Copy link
Author

I think I have two versions of GCMIntentService running.
Restarted my phone, but still same problem.
The "Delivered" notification changed to Acknowledged, but still no notification:

pushnotificationbug3

I'm removing now all other plugins to see if maybe it's caused by another plugin...

From my bug report:

  • com.gamingplatform.tictactoe / u0a238 / v17:
    Process com.gamingplatform.tictactoe (unique, 11 entries):
    SOff/Norm/Top : +24m53s626ms
    Service: +21ms
    Receivr: +125ms
    LastAct: +18m5s806ms
    CchAct : +2h10m22s946ms
    SOn /Norm/Top : +1h31m51s104ms
    Service: +933ms
    Receivr: +383ms
    LastAct: +11m23s594ms
    CchAct : +19m17s755ms
    CchEmty: +36s575ms
    TOTAL : +4h56m32s868ms
    PSS/USS (6 entries):
    SOff/Norm/Top : 6 samples 95MB 100MB 103MB / 89MB 92MB 94MB
    LastAct: 1 samples 77MB 77MB 77MB / 69MB 69MB 69MB
    CchAct : 7 samples 60MB 62MB 69MB / 58MB 59MB 62MB
    SOn /Norm/Top : 53 samples 95MB 107MB 122MB / 88MB 99MB 112MB
    LastAct: 7 samples 67MB 71MB 78MB / 61MB 66MB 69MB
    CchAct : 3 samples 60MB 62MB 64MB / 58MB 60MB 61MB
    myID=bfad412 mCommonProcess=bfad412 mPackage=com.gamingplatform.tictactoe
    Service com.adobe.phonegap.push.GCMIntentService:
    Process: com.gamingplatform.tictactoe
    Running op count 98:
    SOff/Norm: +120ms
    SOn /Norm: +1s957ms
    TOTAL: +2s77ms
    Started op count 49:
    SOff/Norm: +116ms
    SOn /Norm: +1s854ms
    TOTAL: +1s970ms
    Executing op count 98:
    SOff/Norm: +54ms
    SOn /Norm: +463ms
    TOTAL: +517ms
    • com.gamingplatform.tictactoe / u0a240 / v178:
      Process com.gamingplatform.tictactoe (unique, 4 entries):
      SOn /Norm/Top : +15m49s127ms
      LastAct: +41s425ms
      CchAct : +9s865ms
      CchEmty: +173ms
      TOTAL : +16m40s590ms
      PSS/USS (1 entries):
      SOn /Norm/Top : 12 samples 82MB 115MB 129MB / 79MB 107MB 120MB
      myID=e55f7e3 mCommonProcess=e55f7e3 mPackage=com.gamingplatform.tictactoe
      Service com.adobe.phonegap.push.GCMIntentService:
      Process: com.gamingplatform.tictactoe
      Running op count 4:
      SOn /Norm: +73ms
      TOTAL: +73ms
      Started op count 2:
      SOn /Norm: +73ms
      TOTAL: +73ms
      Executing op count 4:
      SOn /Norm: +47ms
      TOTAL: +47ms

2015-12-14 08:23:09.992
com.multiplayer.PostMessage sendPost: Sending POST message to url: https://gcm-http.googleapis.com/gcm/send
Message: {"registration_ids":["fQ3Dg8_jcmM:APA91bEFChZPmKPOD9wjWzj8kxGQ75AVyfYZ0rs4EIt51fByS4yH65XcI5hCrSqjflcLzlXO7y_FAWXX3MdbNvM3acLLG3LZ6YcstTGA2VyWMnXGm6bWtmrKp3JvACYnGUfhy6H2hAUM"],"data":{"title":"New game","message":"yoav local chrome invited you to play","matchId":"2309911837649459429","notificationOpponentId":"5720693702393856","updatedTimestampMillis":"1450099389844","notId":"1899370675","matchInfoJson":"{"gameVariant":0,"matchId":"2309911837649459429","createdTimestampMillis":"1450099389844","updatedTimestampMillis":"1450099389844","isReservedAutoMatch":false,"notificationOpponentId":"0","state":"N4IgLgrgTgdgkjAJgUwB4CFkDMD2VkCyOAbsiAFwAMANONPEmhQIy3JIECGYAxgBYBlHnmQBnCjAgAbKbQC2JZADkIcgEbIoLWlE5IccgcmSIKISgDoArAHZmlACwOAzJQCcV1w8o2ATCFopTlEwIlIKAG1QUWQwABV6ClBIWAQUVBYAX0zqaNikkABrZABPMxDuMlpiTikIMnJQFCkwTgKoHAB3Cl9aYSkKShyQNRxOKFNyCIiQANnaEABdahm5teWZgA01hcXF7MzlkCCQgVawBuBhnmh8GDAzyoKKi4Lm1vaunr6cAaph0bjSbTeagpYrMG7CHbBa7fbDE5gABqAEtRCi1FJkHEcElrrd2Mi0RisTi8ZkgA\u003d\u003d","lastPlayerIndex":0,"nextPlayerIndex":1,"playersInfo":[{"playerId":"5720693702393856","displayName":"yoav local chrome","avatarImageUrl":"http://www.multiplayer-gaming.com/v1/dist/imgs/avatar38.gif","isOnline":true},{"playerId":"4791088564928512","displayName":"Yoav Zibin","avatarImageUrl":"http://graph.facebook.com/10152824135331125/picture?square\u003dsquare","isOnline":true}]}"},"notification":{"title":"New game","body":"yoav local chrome invited you to play","icon":"new"}}
I 2015-12-14 08:23:10.029
com.multiplayer.PostMessage sendPost: POST returned: {"multicast_id":8219156673032702199,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1450099390015797%224af683224af683"}]}

@yoav-zibin
Copy link
Author

No luck --- removed all plugins (except org.apache.cordova.splashscreen, cordova-plugin-whitelist, and phonegap-plugin-push) and still no notifications.

Oh, I think I found it!

12-14 07:59:40.517 1938 2514 D WifiStateMachine: starting scan for "FiOS-SZ05D"WPA_PSK with 5825,2462
12-14 07:59:40.970 3691 22481 I GCM : GCM message com.gamingplatform.tictactoe 0:1450097980753603%224af683224af683
12-14 07:59:40.984 3300 3369 W GcmNotification: Failed to show notification: Icon resource not found: new
12-14 07:59:45.703 1938 21173 D NetlinkSocketObserver: NeighborEvent{elapsedMs=255821266, 192.168.1.1, [485D3633234F], RTM_NEWNEIGH, NUD_STALE}

When I send the notification, I set:
notification.icon = "new"

I'll remove it to use the default icon
(By default the icon displayed in your push notification will be your apps icon. )

@yoav-zibin
Copy link
Author

Yippie, after removing it, android is working fine :)

I couldn't figure out a way to test ios locally on my iphone4, so I'll just submit it to apple and hope for the best, and then test it in production (in 1-2 weeks).

I'll update the bug on whether coldstart is working on ios.
(I'm still skeptical that I'll get "coldstart: true" on ios because I didn't see the string coldstart anywhere in the objective-c code...)

@macdonst
Copy link
Member

@yoav-zibin there is no coldstart for iOS that is an Android GCM supplied piece of data.

@yoav-zibin
Copy link
Author

BTW, finally confirmed that coldstart is working correctly for me on ios using v 1.4.5 (I don't get the coldstart boolean flag, but I can manage without it --- at least I'm getting the notification-handler called :))

(Note that it didn't work in v 1.2.3)

Thanks!

@lock
Copy link

lock bot commented Jun 5, 2018

This thread has been automatically locked.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

3 participants