使用phonegap推送插件在前台推送通知期间仍会显示通知图标

时间:2013-12-13 03:56:00

标签: android cordova

我正在使用phonegap开发一个Android应用程序。我的应用程序工作,我可以在应用程序处于前台和后台时接收推送通知。我面临的唯一问题是,即使应用程序位于前台,通知图标仍会显示。我不希望该通知图标出现在该场景中。有没有办法实现这个目标?

我认为要走的路是修改GCMIntentService类,但我无法弄清楚如何。

我的GCMIntentService类代码:

package com.plugin.gcm;

import com.google.android.gcm.GCMBaseIntentService;
import com.mypackage.MainScreen;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

@SuppressLint("NewApi")
public class GCMIntentService extends GCMBaseIntentService {

    public static final int NOTIFICATION_ID = 237;
    private static final String TAG = "GCMIntentService";

    public GCMIntentService() {
        super("GCMIntentService");
    }

    @Override
    public void onRegistered(Context context, String regId) {

        Log.v(TAG, "onRegistered: "+ regId);

        JSONObject json;

        try
        {
            json = new JSONObject().put("event", "registered");
            json.put("regid", regId);

            Log.v(TAG, "onRegistered: " + json.toString());

            // Send this JSON data to the JavaScript application above EVENT should be set to the msg type
            // In this case this is the registration ID
            PushPlugin.sendJavascript( json );

        }
        catch( JSONException e)
        {
            // No message to the user is sent, JSON failed
            Log.e(TAG, "onRegistered: JSON exception");
        }
    }

    @Override
    public void onUnregistered(Context context, String regId) {
        Log.d(TAG, "onUnregistered - regId: " + regId);
    }

    @Override
    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)
        {
            PushPlugin.sendExtras(extras);

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

    public void createNotification(Context context, Bundle extras)
    {
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        String appName = getAppName(this);

        Intent notificationIntent = new Intent(this, MainScreen.class);
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        notificationIntent.putExtra("pushBundle", extras);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
                .setDefaults(Notification.DEFAULT_ALL)
                .setSmallIcon(context.getApplicationInfo().icon)
                .setWhen(System.currentTimeMillis())
                .setContentTitle("Push Message")
                .setTicker(extras.getString("title"))
                .setContentIntent(contentIntent)
                .setAutoCancel(true);

        String message = extras.getString("message");
        if (message != null) {
            mBuilder.setContentText("You have initiated a transaction. Accept/Deny");
        } else {
            mBuilder.setContentText("<missing message content>");
        }

        String msgcnt = extras.getString("msgcnt");
        if (msgcnt != null) {
            mBuilder.setNumber(Integer.parseInt(msgcnt));
        }

        mNotificationManager.notify((String) appName, NOTIFICATION_ID, mBuilder.build());
    }

    public static void cancelNotification(Context context)
    {
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.cancel((String)getAppName(context), NOTIFICATION_ID);  
    }

    private static String getAppName(Context context)
    {
        CharSequence appName = 
                context
                    .getPackageManager()
                    .getApplicationLabel(context.getApplicationInfo());

        return (String)appName;
    }

    @Override
    public void onError(Context context, String errorId) {
        Log.e(TAG, "onError - errorId: " + errorId);
    }

}

1 个答案:

答案 0 :(得分:0)

尝试用以下代码替换onMessage函数的实现:

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();

        extras.putBoolean("foreground", foreground);

        if (foreground)
            PushPlugin.sendExtras(extras);
        else
            createNotification(context, extras);
    }
}

您还需要以下功能:

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;
}