2013-12-13 57 views
0

我正在開發一個使用phonegap的android應用程序。我的應用程序可以工作,並且我可以在應用程序處於前景以及後臺時接收推送通知。我面臨的唯一問題是,即使應用程序處於前景中,通知圖標仍會顯示。我不希望該通知圖標出現在該場景中。有什麼辦法可以做到嗎?通知圖標仍然出現在使用phonegap推送插件的前臺推送通知

我想要走的路是修改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); 
    } 

} 

回答

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

這個解決方案確實加入以下權限後工作: 但它似乎弄亂了背景中的通知。該消息似乎並未在該場景中設置。 –

+0

這是我的錯誤。我忘了爲!前景值添加PushPlugin.sendExtras()。它的工作現在。非常感謝..:) –