Mike_G
Mike_G

Reputation: 16512

updated PhoneGap now push notifications wont stop

I updated my PhoneGap to 3.1 from 2.9. Everything seems to be working fine except for one thing. I use the PhoneGap push plugin and even if the app is open I get a push notification. It doesnt display the notification icon at the top of the phone, but it does vibrate. This did not happen with PG 2.9. Is there a setting in a config file I am missing?

edit: I was wrong about them not showing up at the top of the phone. If i exit the app, the notifications are displayed as if the app was closed.

Upvotes: 1

Views: 188

Answers (1)

Mike_G
Mike_G

Reputation: 16512

the GCMIntentService had removed the isInForeground method. In Eclipse go to

Project > src > com.plugin.gcm > GCMIntentService.java

and add this code:

public boolean isInForeground()
{
    ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> services = activityManager
            .getRunningTasks(Integer.MAX_VALUE);

    if (services.get(0).topActivity.getPackageName().toString().equalsIgnoreCase(getApplicationContext().getPackageName().toString()))
        return true;

    return false;
}   

then change the onMessage method to read as follows:

protected void onMessage(Context context, Intent intent) {
    Log.d(TAG, "onMessage - context: " + context);

    // Extract the payload from the message
    Bundle extras = intent.getExtras();

    if (extras != null)
    {
        boolean foreground = this.isInForeground();

        PushPlugin.sendExtras(extras);

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

In the AndroidManifest.xml you will need to add the following permission:

<uses-permission android:name="android.permission.GET_TASKS" />

Upvotes: 1

Related Questions