phonegap / phonegap-plugin-push

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

Android Action Buttons - Foreground false - Execution Time #1682

Open ElNinjaGaiden opened 7 years ago

ElNinjaGaiden commented 7 years ago

I have one notification with 2 actions buttons: "accept" and "reject" an invitation to an event. The actions are configured with foreground = false because the goal is to call my web API with the respective user response without the need to open the app.

Expected Behaviour

Get the javascript callback called immediately when the user hits one of the two action buttons shown on the notification.

Actual Behaviour

The javascript callback doesn't get called when the user hits one of the two action buttons. It gets called on the next app start. It seems the javascript can't be executed outside the context of the app.

Reproduce Scenario (including but not limited to)

For Android devices sent a payload with actions configured as foreground = false The notification shows the buttons but doesn't execute the javascript code until the app gets started

Steps to Reproduce

Platform and Version (eg. Android 5.0 or iOS 9.2.1)

Android 6.0.1

(Android) What device vendor (e.g. Samsung, HTC, Sony...)

LG Nexus 5

Cordova CLI version and cordova platform version

cordova 6.5.0
android 5.1.1

Plugin version

1.8.4

Sample Push Data Payload

Sample Code that illustrates the problem

Currently both functions just call the simplest function on the API which logs an entry on a log file but we see this call gets executed when the app starts and not when the action button is clicked.

function acceptFriendlyGame (data) {
    var appConfigData   = window.environments[window.environment],
        apiUrl          = appConfigData.ApiUrl,
        url             = apiUrl + '/core/ping',
        options         = { method: 'GET' };

    fetch(url, options);
}

function rejectFriendlyGame (data) {
    var appConfigData   = window.environments[window.environment],
        apiUrl          = appConfigData.ApiUrl,
        url             = apiUrl + '/core/ping',
        options         = { method: 'GET' };

    fetch(url, options);
}

Logs taken while reproducing problem

androidlogcat

macdonst commented 7 years ago

@ElNinjaGaiden do you put your app in the background or do you force close it?

ElNinjaGaiden commented 7 years ago

@macdonst I have the app closed

macdonst commented 7 years ago

@ElNinjaGaiden minimized, i.e. in the app stack or have you force closed it?

ElNinjaGaiden commented 7 years ago

@macdonst totally closed. Not in the app stack.

macdonst commented 7 years ago

@ElNinjaGaiden right, so in that case there is no web view loaded in memory in order to run the JS code which is why it is cached until the next time the app starts.

fredgalvao commented 7 years ago

@ElNinjaGaiden you can probably take advantage of this, though: https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#application-force-closed

ElNinjaGaiden commented 7 years ago

@macdonst ok that makes sense, I just wanted to confirm. It will be nice if something like that could be done in a future version of the plugin.

By the way, your plugin rocks, thanks for the great hard work.

ElNinjaGaiden commented 7 years ago

Oh thanks @fredgalvao I'll take a look. As the documentation say, I'm behind the cordova-android version which supports that feature but definitely I'm going to give it a try.

macdonst commented 7 years ago

@ElNinjaGaiden glad you are enjoying the plugin.

ElNinjaGaiden commented 7 years ago

@fredgalvao there is something like your proposed solution for force closed apps for iOS?

fredgalvao commented 7 years ago

Not yet implemented, IIRC. I haven't heard from @macdonst of plans to implement it either, but only him can speak for himself.

ElNinjaGaiden commented 7 years ago

@fredgalvao ok, thanks.

And regarding the Android implementation, I'm seeing an odd behaviour.

I have a notification with action buttons configured to run the callback on the background th way you show me: cordova 6.1.2 to support the feature and I'm sending "force-start = 1" on the payload.

That part seems to work fine. When I get the notification and I press one of the action buttons the app starts (in case it was force closed), run on the background and it process my callback and all my logic fine.

At this point the app keeps on the background.

But lets say it receives a second notification (with no action buttons), just a normal notification. The app thinks it is on foreground and it handles like it is on foreground. Actually I see "foreground = true" on this second notification into the data sent by the plugin, but the app is on background.

Any ideas?

ElNinjaGaiden commented 7 years ago

My dummy guess the problem is because of this:

If the app is forced close, when the first notification arrives, it starts the app and initialize the plugin, running this:

@Override
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);
        gForeground = true;
    }

Actually if the app is forced close you can see it start very quickly and "go to background", but the onPause event is never fired, and the gForeground flag remains true.

That's why when the second notification arrives, the plugin thinks it is on foreground. Again, this is just a dummy guess. I'm just starting to check the java code of the plugin (and I'm not a java expert)

fredgalvao commented 7 years ago

That seems like an inconsistency indeed, I can't really help you there though. @macdonst or @jcesarmobile would have to help on this one.

macdonst commented 7 years ago

@ElNinjaGaiden seems like a bug. I will try and find some time to reproduce and fix but PR's are always welcome.

sundaramoorthyjeeva commented 7 years ago

@fredgalvao I am a starter of Ionic and I am exploring phonegap push plugin. I need some information from you. As of my understanding you have done the notification action button triggered. My requirement is to add two action buttons in the notification and click on action button, the callback method should trigger and show something like toast or alert. Callback not getting triggered in my app. what is necessary payload structure to link the callback methods. Can you help me to achieve my requirement.

fredgalvao commented 7 years ago

@sundaramoorthyjeeva I didn't actually use action buttons on my applications, I was just going from what I know of the project and the documentation to help on the issue.

If you have followed the documentation yourself and weren't able to get it to work, please create a new issue and provide as many details as possible, so people can maybe help you.

ElNinjaGaiden commented 7 years ago

@sundaramoorthyjeeva I do have some experience working with action buttons. Not that much but some, maybe I could help. Is your callback not getting triggered on Android, iOS or both? It's a little bit different for both platforms.

JdHondt commented 4 years ago

Has this situation changed in any way since the last activity on this issue? I have the exact same problem/intent as @ElNinjaGaiden. However, I'm not sure the user will appreciate it that the app is force-started in the background when he/she clicks on a button; which is the result of implementing the suggestion of @fredgalvao to send a notification with force-start: 1. In my case, I would like to be able to retrieve the timestamp the user clicked on the action-button and save it to the db using a simple API call at the time of clicking.