phonegap-build / PushPlugin

This repository is deprecated head to phonegap/phonegap-push-plugin
https://github.com/phonegap/phonegap-plugin-push
MIT License
1.31k stars 1k forks source link

No notifications in android when the app is closed #85

Closed Chexpir closed 10 years ago

Chexpir commented 10 years ago

In GCMIntentService.java:73 we see they check if the app is in foreground

// Send a notification if there is a message and not in foreground if (!PushPlugin.isInForeground() && extras.getString("message").length() != 0) {

, but that #79 change doesn't work correctly in android due to the ActivityManager starts the app in the moment the notification is received, thus when the app is completely closed there is no notification.

10-08 11:56:16.042: I/ActivityManager(525): Start proc com.myapp.mainClass for broadcast com.myapp.mainClass/com.plugin.gcm.CordovaGCMBroadcastReceiver: pid=25076 uid=10112 gids={50112, 3003, 1028} 10-08 11:56:16.072: V/GCMBroadcastReceiver(25076): onReceive: com.google.android.c2dm.intent.RECEIVE 10-08 11:56:16.072: V/GCMRegistrar(25076): Setting the name of retry receiver class to com.plugin.gcm.CordovaGCMBroadcastReceiver 10-08 11:56:16.072: V/GCMBroadcastReceiver(25076): GCM IntentService class: com.plugin.gcm.GCMIntentService 10-08 11:56:16.082: V/GCMBaseIntentService(25076): Acquiring wakelock 10-08 11:56:16.082: V/GCMBaseIntentService(25076): Intent service name: GCMIntentService-GCMIntentService-1 10-08 11:56:16.092: D/GCMIntentService(25076): onMessage - context: android.app.Application@41dce920 10-08 11:56:16.092: V/PushPlugin(25076): sendExtras: caching extras to send at a later time. 10-08 11:56:16.092: D/GCMIntentService(25076): onMessage - foreground: true 10-08 11:56:16.092: D/GCMIntentService(25076): onMessage - message: ? Peace,Love ? and PhoneGap ?! 10-08 11:56:16.092: V/GCMBaseIntentService(25076): Releasing wakelock

paulroehr commented 10 years ago

I'm encountering the same problem. As a little workaround I added the boot permission to the receiver, so the app is always in the background but that didn't worked, too.

I hope there will be a fix soon.

olegongit commented 10 years ago

Same here. The new check for foreground returns true if the app was closed (swiped away) on Android.

simonihmig commented 10 years ago

Some for me. If I remove "!PushPlugin.isInForeground() &&" from line 73, I will always get the notification in the notification list, even when the app is completely unloaded (also when the app is in the foreground, which is not desirable of course). Without that, notifications will not work when the app is not running. Hoping for a fix...

ghost commented 10 years ago

I have the same problem using the Phonegap Build service. Everything is working except when the App is completely closed.

cuatl commented 10 years ago

in file PushPlugin.java add gForeground=false; on Destroy() function.

ghost commented 10 years ago

having a struggle here as well, thought about using the background-service. @cuatl I've tried your fix, it doesn't seem to work.

macris commented 10 years ago

Hi @eyalOasis , in file GCMIntentService.java line 73 you can see this lines: if (!PushPlugin.isInForeground() && extras.getString("message").length() != 0) { createNotification(context, extras); }

When application killed via application manager PushPlugin.isInForeground() return true. Also Destroy() function does't run if application killed. Thats why @cuatl sollution doesn't work if application killed. So you can try to delete "!PushPlugin.isInForeground() &&" in line 73. So every time you get push, GCMIntentService call createNotification(). In this case you get notification even if application is running.

Emadello commented 10 years ago

Dear All,

I have made a work around which works actually, here's the steps you need to do and changes to make:

  1. In your mainactivity of the application (Lets name it Activity1), create public static boolean isinFG = false;
  2. in your mainactivity of the application, add the following code:

    public void onPause() { super.onPause(); isinFG = false; }

    public void onResume() { super.onResume(); isinFG = true; }

    public void onDestroy() { super.onDestroy(); isinFG = false; }

  3. in GCMIntentService.java, goto onMessage function, and replace the line:

if (!PushPlugin.isInForeground() && extras.getString("message").length() != 0) { createNotification(context, extras); }

with

if (!Activity1.isinFG) { if (extras.getString("message").length() != 0) { createNotification(context, extras); } }

Cheers

ghost commented 10 years ago

Correct me if I am wrong. None of the solutions above can be used if I use the Phonegap Build service (https://build.phonegap.com)?

Thank you.

denboogert commented 10 years ago

I'm experiencing the same issue using Phonegap Build version 3.0.0 (Android; the latest supported version).

The notification appears in the status bar when the app is running in the background, but it does not show when the app is closed. I'm hoping to use the notifications for a reminder service for a library, so I really look forward to a solution. Thank you.

felipenova commented 10 years ago

Hello My app works perfectly with the plugin, even when my app is in the background process in the cache, but after a certain time even standing still in the background process in the cache it does not get more notifications until I reopen my app. I do not know why it happens and I do not know what to do, please help me.

cutzenfriend commented 10 years ago

Same Problem here! Android. And im also using phone gap build service! Please Fix that issue :(

fedme commented 10 years ago

Same problem here, may I ask if this project is actually maintained active by some developers or it's dead? In this unlucky case, are there any alternatives? I'm sorry I cannot help I'm not such an expert coder yet

jorgecis commented 10 years ago

I was thinking the same, is this project dead? 14 developers and nobody cant reply, I can fork this project and try to fix some issues like this one, but the problem will be when somebody try to use my repository in the build phonegap service.

Can some developer give me permission to do commit to this repository?

fedme commented 10 years ago

I think you could fork this and then submit your own repo to phonegap build from here https://build.phonegap.com/plugins#add

bobeast commented 10 years ago

@jorgecis (and others) PushPlugin is an Open Source project, and as such relies heavily on the developer community. The PhoneGap Build team has been busy adding support for newer versions of Cordova, WinPhone8, infrastructure improvements, etc. Alas there are only so many hours in the day.

This is where you come in. To contribute to this plugin, just fork the repo and make your changes to that fork. Then submit a pull request, explaining the nature of the change in the submission comments.

If you get to a solution before we do, it is a win for everyone. Thanks! Bob

scotthooker commented 10 years ago
scotthooker commented 10 years ago

I've wrote some test cases around this and can confirm the following

  1. No notifications are received when the app is closed.
  2. Notifications are received in a short time after the app is minimised
  3. Notifications are received when the app is open
beeni commented 10 years ago

Same here...

scotthooker commented 10 years ago

Beeni are you using phone gap build or just phone gap?

beeni commented 10 years ago

Unfortunately PG Build - 3.0 at the moment, haven't tried 3.1 yet.

scotthooker commented 10 years ago

Ok - it's the push plugin for PG 3 + thats the problem.

PG 3.1 doesn't fix this issue.

Need to work on getting a patch into this and getting it pushed in PGB ASAP

scotthooker commented 10 years ago

Emadello solution works I think but I can't test this with phone gap build. I'm not sure of the release process with phone gap build either. How do updates in this module/plugin get released there?

bobeast commented 10 years ago

Any solution which requires the main activity to be edited will not work on Build. The entire solution needs to be encompassed within the plugin itself.

For what its worth, Community solutions are welcome in the form of Pull Requests. Those typically make it onto build faster than waiting on the build team to solve the problem.

scotthooker commented 10 years ago

Ok Cheers Bob.

I'll take a starting point at working out what changed between PG2.9 -> PG3+ as this plugin was working in the earlier version. It's one of the most used on Build so I imagine this issue will start to affect more people upgrading to 3.0.

bobeast commented 10 years ago

One thought that occurs to me is that it might be as simple as adding

    gForeground = false;

in the OnDestroy method of PushPlugin.java

worth a shot...

scotthooker commented 10 years ago

I'll test that.

Bob - do you know the build patch process. Who can submit a patch to the version on build?

I don't mind writing and testing a pull request but not sure who the maintainers are

bobeast commented 10 years ago

Scott,

For the PhoneGap owned plugins (like this one), The process is that I will validate the pull request, and will then merge it into main and update the version on Build.

For community owned plugins, they can be submitted and/or updated in the plugins section of the web app. Once those submissions hit, they go through a review process similar to Apple's where they will be accepted or rejected depending upon the review outcome. If accepted, they will go live on Build after acceptance.

scotthooker commented 10 years ago

Ok gotcha. We have a similar process at drupal.org.

Looking through the issue queue this is the only bug I would class as major. Will try out the fix you suggested.

scotthooker commented 10 years ago

Ok so

line 34 of PushPlugin.java: private static boolean gForeground = true;

was added 4th October 2013 and later when the

line 116: of PushPlugin.java: extras.putBoolean("foreground", gForeground);

this would probably be where the logical problem is.

Not sure its worth me doing a pull request for that one line change on 34.


Cheers for explaining the process Bob :) every type of open source project has its workflow

scotthooker commented 10 years ago

Easiest fix is therefore

public void onDestroy() 
{
    GCMRegistrar.onDestroy(getApplicationContext());
    gWebView = null;
    gECB = null;
            gForeground = false;
    super.onDestroy();

}
scotthooker commented 10 years ago

Sorry for the mass of messages. I've created a pull request for this.

bobeast commented 10 years ago

Thanks Scott, Did you get a chance to verify that this solves the issue? It looks like a day or so before I can carve out some time to test it. (5 minute solution = 2 hour test)

BTW, thanks a bunch for stepping up.

scotthooker commented 10 years ago

My debugging tools for android are limited so I can't get a full console output to see if anything untoward is going on. All I can see if whether the push notification gets received when closed and doesn't when it opens. So yes it solves the issue. Although like you say I can't run the tests.

Thanks. It's not a problem, :) - I will be first to say that java and objective C isn't my area of expertise! I'm a php man ;)

bobeast commented 10 years ago

OK. I'm going to take a leap of faith and post this on build.

Thanks again. This is what open-source is all about.

scotthooker commented 10 years ago

Grrrr must read more stuff around how Android works

Destroy() function does't run if application killed. So the pushplugin still works when the app is minizmied but still not when its force closed.

Must be something that gets run on a push receive letting it know the status of the app. The problem is the app seems to always think its in the foreground unless its open and minimised.

Anyone with more Android knowledge care to help me out :)

scotthooker commented 10 years ago

Probably something in GCMIntentService.java

if (!PushPlugin.isInForeground() && extras.getString("message").length() != 0) { createNotification(context, extras); }

The app thinks its in the foreground. I guess the behaviour needs to be reversed so the app default is its in the background. But gets set to foreground otherwise.

Talking it through now it probably is that simple... Will do another patch unless anyone wants to step up ;)

Sorry for this trial and error approach. As I said Android and Java isn't my usual method of programming! I am testing all this to make sure nothing breaks though.

scotthooker commented 10 years ago

https://github.com/phonegap-build/PushPlugin/pull/100

Can someone review that. If we initially set the app to be in foreground false. We will ALWAYS receive notifications and we just rely on the other functions to state the app is in the foreground.

This is the only way and the way it should be done. We can't rely on the app having set foreground FALSE. Where as we can rely on the app setting foreground TRUE.

felipenova commented 10 years ago

Now even when the app is closed usually works but after some time no longer works, until the app is reopened.

Emadello commented 10 years ago

Guys, my solution above is tested and works on cordova 2.9 (not cordova build)

setting foreground from PushPlugin is just wrong, you have to set it at activity level

felipenova commented 10 years ago

I realized that when my screen is locked not get more notifications until I reopen the app. Is this normal?

felipenova commented 10 years ago

I tested the tip @ Emadello and apparently worked with cord 2.9 even when the app is with the screen locked. Thanks @ Emadello. Now this solution should be sent to operate PG Build.

Emadello commented 10 years ago

you're welcome :)

bobeast commented 10 years ago

@felipenova - the solution provided by felipenova requires edit of the app's root activity. This will not work on Build. The entire solution MUST be encapsulated in the plugin for it to work on Build.

That said, its good to hear we have a solution for our non-Build users. Thanks felipenova!

felipenova commented 10 years ago

Got it, but whoever was solved Emadello I just tested to see if it really worked. Thank you.

sudhyr commented 10 years ago

Not so correct but this hack seems to work for me.. GCMIntentService.java onMessage() function..

// Send a notification if there is a message and not in foreground
// if (!PushPlugin.isInForeground() && extras.getString("message").length() != 0) {
//  createNotification(context, extras);
// }

// modified is app in foreground checker
if ( (!PushPlugin.isInForeground() || !PushPlugin.isActive()) && extras.getString("message").length() != 0) {
    createNotification(context, extras);
}

PushPlugin.isActive() checks weather the webview is active

scotthooker commented 10 years ago

edewit has reviewed my patch/ pull request

https://github.com/phonegap-build/PushPlugin/pull/100 here

This should take care of all the issues. I need someone to test it properly. I stepped it all through last night and edewit reviewed the code. Just need someone to test it on a device.

Basically changing

private static boolean gForeground = true; to private static boolean gForeground = false;

and then adding the following activity markers

@Override public void onStart(boolean multitasking) { super.onStart(multitasking); gForeground = true; }

@Override
public void onRestart(boolean multitasking) {
    super.onRestart(multitasking);
    gForeground = true;
}

@Override
public void onStop(boolean multitasking) {
    super.onStop(multitasking);
    gForeground = false;
}

Fixes the problem as we can rely on the the other activity functions setting foreground to be true

scotthooker commented 10 years ago

Or Rihdus's fix probably works.

Either way this is quite important to be fixed I think for any PGB users downloading the plugin.

cutzenfriend commented 10 years ago

Is this fix now already on Phonegap Build or not?

scotthooker commented 10 years ago

Not yet. Need someone to test my patch in #100 and that should fix it.