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

"Notification" Event Not Firing When Closed Through App Launcher #158

Open
ryanpager opened this issue Sep 21, 2015 · 70 comments
Open

"Notification" Event Not Firing When Closed Through App Launcher #158

ryanpager opened this issue Sep 21, 2015 · 70 comments

Comments

@ryanpager
Copy link

So this is the use case im following:

a. Push notification goes to phone
b. User clicks on notification from lock, which opens the app & triggers the 'notification' event
c. Do some obligatory action based on additionalData

This is working perfectly, except it seems like the 'notification' event does not get triggered ever again if the user closes the application through the app launcher (square button & swipe) and the app has to coldstart.

So a run down of the problem in short:

a. User opens application, push is registered.
b. 'Notification' events now get triggered as expected.
c. User closes the application through the app launcher.
d. 'Notification' events no longer get triggered....ever.

Any ideas on this one?

@ryanpager ryanpager changed the title "Notification" Event Not Firing "Notification" Event Not Firing When Closed Through App Launcher Sep 21, 2015
@fredgalvao
Copy link
Collaborator

Is this on Android?

@ryanpager
Copy link
Author

Yea, I'm testing this on Android 5.1.1 on a Nexus 5.

I also tried reregistering the device on cold start which fixes the future
notification events (great) but not existing ones already issued.

@macdonst
Copy link
Member

@ryanpager after you force close the app do you still see the new push messages in the notification shade?

@ryanpager
Copy link
Author

@macdonst Oddly enough yea -- push notifications still work exactly as expected, they just dont trigger the 'notification' event regardless of if the app is in foreground or background. I also get them if the phone is completely asleep / locked (show up on the homescreen like normal).

@macdonst
Copy link
Member

@ryanpager Okay, that's actually the behavior I would expect. When you close the app through the app launcher you are effectively killing your app so the on('notification') handler has been removed. I'm going to look at waking up the app on incoming notifications in the future but for now this is not handled.

PR's on this topic are welcome.

@NkHAp
Copy link

NkHAp commented Sep 26, 2015

@ryanpager I am also having similar issue and using phonegap build so can you tell me how you able to receive notification even after force close.

@ryanpager
Copy link
Author

@app2naklabs I just installed the plugin and registered it like normal. But I am not using phonegap, its a cordova+ionic setup (which i dont think makes too much of a difference). The only different thing im doing is that im re-registering the device on coldstart. The push notification setup is AWS SNS (Amazon) => GCM (Google) => Phone. I havent tried any of this on iPhone yet.

@NkHAp
Copy link

NkHAp commented Sep 29, 2015

Can you please tell me how you are doing that; because when app is completely closed and not in used then in between if any notification is triggered then its not appearing in my device. So If you can tell me what changes you made in .js file that helped you in getting this issue solved. I am using phonegap build for building my app.

@ryanpager
Copy link
Author

@app2naklabs I dont know how to explain this any better. I am 100% using the stock plugin, with no extra configuration settings, and literally no changes to any file. It just works right out of the box the way I said it does. The only difference between yours and mine is that im using cordova/ionic versus phonegap.

Sample Code:

settings =
  android:
    senderID: xxxxxxxxxxxxxx
  ios:
    alert: yes
    badge: yes
    sound: yes

# Create instance
pushInstance = PushNotification.init(settings)

# Handle different events
pushInstance.on 'registration', _onRegistration
pushInstance.on 'notification', _onNotification
pushInstance.on 'error', _onError

Plugin Version: 1.2.3

@Mojo90
Copy link

Mojo90 commented Oct 25, 2015

From my point of view:
Using also cordova.
On Android everything works fine! Even when App is closed and than cold started through a notification the on notification is triggered. And of course Push Notifications are received at anytime. (As known by other Threads I am using Parse.com as Backend.)
BUT on iOS the cold start does unfortunately not trigger the on notification. I am very interested though that also on iOS it will be triggered. If iOS App is in Background everything just works fine as on Android but once App is closed and App has to cold start than on notification is not triggering (thus I came to this thread).
I'll have a deeper look at it next week but in case somebody figures this out before pls let me know :).

And yes I am also using Stock Plugin 1.2.3 except I did some customizing on Android for Parse.com which can be read here: #147 (but I think this customizing has nothing to do with working on notification trigger on Android cold start)

UPDATE:
Just tried V1.3.0 after reading the changelog --> seems to be fixed now on iOS as well, thx @macdonst :)

@kunalnagar
Copy link

@ryanpager Facing a similar issue. The .on('notification') handler is not invoked if the app has been closed from the multi-tasking pane on Android. Instead, it starts off with the device ready code and functions normally as if it was a normal app start.

@EduardMalik
Copy link

+1
Android 5.1
.on('notification') not fired when application closed

@goodthing
Copy link

Using ionic here.

Experiencing similar issue when quitting the app in ionic regardless of whether i quit through task manager or through the program close.

I have a login in my app so it needs to check auth first before redirecting to the relevant page based on the notification that was clicked. The actual handling of notificationReceived is in the controller after login.
I'll try and move it to before login and see if it fires.

Does register/init need to happen before notificationReceived, if the device was already registered in a previous session?

@uriva
Copy link

uriva commented Feb 20, 2016

++

@jianhao1203
Copy link

Is this enhancement already done?

@ghost
Copy link

ghost commented Mar 23, 2016

Thanks @macdonst.

I get nothing on android (not even a system notification) on a Zenfone 2, Android 5.0 stock rom.

this is the logcat:

I/GCM     (19955): GCM message com.test_app.mobile 0:1458748156761978%e60cf8f4f9fd7ecd
W/BroadcastQueue(  673): Reject to launch app com.test_app.mobile/10241 for broadcast: App Op 48
W/GCM-DMM (19955): broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.test_app.mobile (has extras) }

https://github.com/phonegap/phonegap-plugin-push/issues/748

@ghost
Copy link

ghost commented Mar 25, 2016

I fixed my issue.. I was sending everything on Data.. that only works when the app is running. I filled the notification object and now it works flawlessly. Thanks @macdonst .

@superainovacoes
Copy link

+1 for notification triggers when app is closed
also other events like receive/open/dismiss the notification ( more info at #756 )

@marcoziegaus
Copy link

+1 for notification event in JS when the app is killed/terminated.

I found this (restarting the MainActivity if the WebView is not started) somewhere, maybe you could utilise a similar approach?

@hariharanneo
Copy link

@haclairvoyant i am not tried with the latest version..my point is notification event triggered when user click on notification

@macdonst
Copy link
Member

macdonst commented Aug 4, 2016

@haclairvoyant no, there is no solution for this currently. If the user kills you app you will not get the JS event fired. There is one possible way to fix this but it requires a change in cordova-android that I'm trying to get in.

@murillobarata
Copy link

This issue was solved?

@ominfowave
Copy link

i have same issue.
Do you have any solution?

@ricardokdz
Copy link

I have same issue on iOS.

Exists good alternative for this plugin?

@davigonz
Copy link

Same issue on Android 6.0 & iOS 8.1.3.

Any solution with Ionic + Cordova? Thank you

@murillobarata
Copy link

Does anyone know that other plugin can do this type of notification?

@Muji86
Copy link

Muji86 commented Sep 17, 2016

If the app is is in the background and swiped away in Android you need to send ("conent-available': 1) in the payload. This will wake up the app and run the notification callback. It will also work once you restart the phone as your app will still be in the background.
If you force kill the app from app manager you will not receive a notification full stop. This is normal android behaviour and it's also true for all apps like WhatsApp.

macdonst added a commit that referenced this issue Nov 2, 2016
…ncher

* Use Cordova-Android 6 for start in background feature
* Finish start in background
macdonst added a commit that referenced this issue Nov 2, 2016
…ncher

* Use Cordova-Android 6 for start in background feature
* Finish start in background
* Update docs
@macdonst
Copy link
Member

macdonst commented Nov 2, 2016

Okay, if you use cordova-android 6.0.0 or greater and grab the latest from master then you should be able to send force-start: 1 as detailed here: https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#application-force-closed

@macdonst macdonst added this to the Release 1.9.0 milestone Nov 2, 2016
@AlexisSerneels
Copy link

I've seen Release 1.9.0 introduced the force-startparameter on Android.

I confirm this is still an issue on latest iOS. Notification is not processed when application was "killed". Even combined with content-availablepayload.

@TrevorPage
Copy link

TrevorPage commented Nov 16, 2016

I don't mean to hijack the topic, but I would like help in understanding whether I actually need to use force-restart to mimic the behaviour I would have on a native application.

On a native Android application, I would expect a couple of important things: (1) After a device restart, it's possible to configure the GCM Service to start up on device boot. This means that the application code can handle the notifications even if the user hasn't started the app from the launcher since boot. (2) If the user backgrounds the app, then even if the associated Activitys are killed by the OS, the Service will stay, meaning that push notifications are still handled by the application code.

Now, when it comes to an Ionic application using this library, the things I would like to know are:

(1) When the Ionic application is "in the background", presumably the OS may eventually kill it. But, will the push callback still stay around in a Service? Or, do I need to rely on force-start always just in case the app has been killed? As a native Android developer, the use of the term "background" in the lifecycle flowchart for this plugin worries me, because I'm used to expecting backgrounded code to be very short-lived.

(2) Do I need to also rely on force-restart to ensure the callback is called if the user has never started the application since boot, or does the plugin/framework already handle this (perhaps using android.intent.action.BOOT_COMPLETED)?

EDIT

I now have a better understanding, so don't need answers to the above. The answers I've determined are:

(1) I continue to assume that a backgrounded Cordova app will live for a long time, but may at some point be killed. This is just my assumption. Therefore I do rely on the force-start:1 property this library offers to ensure the app is force started into background if it's not running at all.

(2) Yes. If the user has never started the app, then again force-start:1 is needed to start the app into background so that the callback may be called. As per the flowchart, content-available:1 is also required.

All of this is based on Android experience only so far. I've not yet dealt with iOS.

@VinceOPS
Copy link

VinceOPS commented Nov 25, 2016

@macdonst Hi Simon! Is this new feature force-start supposed to also trigger the notification handler? Because it doesn't seem to trigger it, at least in my app. I do receive the notification. It does (re)start the app, but the notification handler isn't triggered. Am I supposed to combine it with content-available, or something?

Btw, thanks for this plugin and the very hard work!

NB:

  • Plugin version: 1.9.0
  • Device: Samsung Galaxy A5 (2016) with Android 5.1
  • Cordova version: 6.4.0
  • Cordova-android version: 6.1.0

EDIT I did need to use content-available, which, as highlighted by Trevor, makes sense when we look at the flowchart in the documentation.
It seems to work well, except if I click the local notification BEFORE the app fully background started (in that case, the notification handler is not triggered).

@TrevorPage
Copy link

TrevorPage commented Nov 25, 2016

@VinceOPS I'm successfully using force-start to start the application into the background and I am combining it with content-available. Also, my notification intentionally does NOT contain a message or title property. All of these factors ensure that the library does NOT automatically create a local notification for you, and This ensures the handler is always called, whether the application is in foreground, background, or started into background. This is tested by me on Android so far (not iOS yet).

(As an aside while I'm here, I encourage anyone using force-start to see this issue I raised, which highlights that you must be really careful that your Android really has been properly updated: #1402 (comment))

@TrevorPage
Copy link

@VinceOPS Another way of putting it, is that even when force-start is used, the flowchart here still applies:

https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md

So, content-available is needed in the situation where you want the callback to be called when the application is in the background (which is the case when it has been force-started).

The reason I personally don't have a title or message is because I only want my callback to handle the message; I don't want automatic local notifications from the push library. However these don't affect whether the callback is called (contrary to what I said above).

Hope that helps :)

@VinceOPS
Copy link

VinceOPS commented Nov 25, 2016

@TrevorPage: Hi! Thanks for your answer. Unfortunately, I do need to pass a title/message with this push notification... Yup I checked again and I'm pretty sure that my cordova-android is up to date, with the proper MainActivity!

Ok, saw your new post in live. The GitHub team is doing things properly right? :P.
I'm going to try right now with content-available and no title/message. But then, how do you manually trigger a local notif? You just don't? My use case is simple: the app may have been killed, and the user receives a new message. A push notif is sent. I'd like him to be able to open the app directly on the message (that's why I need the notification handle to be triggered).

Thanks again ;)

@TrevorPage
Copy link

@VinceOPS Just to ensure you noticed it, beware that I've corrected my earlier wrong comment about the title/message. Whether or not these are present do not affect whether the callback is called; only content-available affects the callback being called.

Having a title or message in the notification causes the push library to automatically create a local notification for you if, and only if, the application is not in the foreground.

I, personally, omit the title and message from the notification because I don't like this behavour. For reasons specific to my application, I always want the notification to be processed by my own code before any local notification is issued. Therefore, I use $cordovaLocalNotification to create a local notification, if I want to create one. As I'm not using message and title, I put all my application specific data in custom property names.

If for your purposes you are happy for a push notification from the server to always directly cause a local notification based on the message/title when the app isn't in foreground (i.e. it's background or being force-started into background) then you could make use of message and title.

@VinceOPS
Copy link

Yup, just noticed that content-available was helping triggering the notification handler, even with Title/Message, indeed.
I also noticed that if I click the local notification too quick (before the app actually fully background started, I guess), the handler is not triggered. But this is not a big deal, at the moment.

Thanks again!

@TrevorPage
Copy link

@serneelsalexis Regarding your observation on iOS, is there any possibility that you have the iOS equivalent to the problem I had here: #1402 (comment)

In other words, rebuild the iOS platform using the very latest platform version?

@fredgalvao
Copy link
Collaborator

@TrevorPage , cordova-ios doesn't have that same feature available yet (start on background). I may be wrong though, it's been a week since I caught up on the dev mailing list.

ippeiukai pushed a commit to ippeiukai/phonegap-plugin-push that referenced this issue Feb 1, 2017
… App Launcher

* Use Cordova-Android 6 for start in background feature
* Finish start in background
* Update docs
@uj
Copy link

uj commented Feb 5, 2017

I can confirm that force-start feature is working great on Android. Will this be available in ios? To be clear, I am referring to the force-start ability for a push notification to start the app when the app is not running (like after a reboot). It does not matter too much if it opens the app in the foreground or in the background, although opening it in the background would be preferable.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests