2013-12-13 2 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 기능의 구현을 교체하십시오 :

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

이 솔루션을 수행합니다 당신은 또한 다음과 같은 기능이 필요

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

을 다음 권한을 추가 한 후 작업 : 하지만 백그라운드에서 알림을 엉망으로 만든 것 같습니다. 이 시나리오에서는 메시지가 설정되지 않은 것처럼 보입니다. –

+0

그것은 저의 실수였습니다. 나는 전경 값을 위해 PushPlugin.sendExtras()를 추가하는 것을 잊었다. 그 지금 일하고있어. 고마워. .. :) –