2017-06-22 71 views
1

我在我的android應用程序中使用FCM通知。一切正常。當應用程序打開時不顯示FCM通知

我的問題是,我不想顯示通知,如果應用程序是打開的。

這裏是我的FirebaseMessagingService

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService{ 

    public static final int NOTIFICATION_ID = 1; 
    private NotificationManager mNotificationManager; 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 

     showNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("message"),remoteMessage.getData().get("type") 
       ,remoteMessage.getData().get("src"),remoteMessage.getData().get("productid"),remoteMessage.getData().get("productname") 
       ,remoteMessage.getData().get("categoryname"),remoteMessage.getData().get("categoryid")); 
    } 

    public Bitmap getBitmapFromURL(String strURL) { 
     try { 
      URL url = new URL(strURL); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      connection.setDoInput(true); 
      connection.connect(); 
      InputStream input = connection.getInputStream(); 
      Bitmap myBitmap = BitmapFactory.decodeStream(input); 
      return myBitmap; 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return null; 
     } 
    } 

    private void showNotification(String ttl,String msg, String type, String src, String pid, String pname, String cname, String cid) { 

     mNotificationManager = (NotificationManager) 
       this.getSystemService(Context.NOTIFICATION_SERVICE); 

     Intent notificationIntent = new Intent(this, MainActivity.class); 
     Bundle extras = new Bundle(); 
     extras.putString("productFragment","productItem"); 
     extras.putString("productId",pid); 
     extras.putString("productName",pname); 
     extras.putString("categoryName",cname); 
     extras.putString("categoryId", cid); 
     notificationIntent.putExtras(extras); 

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

     if (type.matches("text")) { 
      Bitmap bitmap_logo = BitmapFactory.decodeResource(this.getResources(), R.drawable.mos); 
      Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
      NotificationCompat.Builder mBuilder = 
        new NotificationCompat.Builder(this) 
          .setContentTitle(ttl) 
          .setContentText(msg) 
          .setLargeIcon(bitmap_logo) 
          .setSmallIcon(R.drawable.mos) 
          .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) 
          .setAutoCancel(true) 
          .setSound(notificationSoundURI); 
      mBuilder.setContentIntent(contentIntent); 
      mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
     }else if(type.matches("image")){ 
      Bitmap bitmap_logo = BitmapFactory.decodeResource(this.getResources(), R.drawable.mos); 
      Bitmap bitmap_image=getBitmapFromURL(src); 
      Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
      NotificationCompat.Builder mBuilder = 
        new NotificationCompat.Builder(this) 
          .setContentTitle(ttl) 
          .setContentText(msg) 
          .setLargeIcon(bitmap_logo) 
          .setSmallIcon(R.drawable.mos) 
          .setStyle(new NotificationCompat.BigPictureStyle() 
            .bigPicture(bitmap_image) 
            .setBigContentTitle(ttl) 
            .setSummaryText(msg)) 
          .setAutoCancel(true) 
          .setSound(notificationSoundURI); 
      mBuilder.setContentIntent(contentIntent); 
      mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
     } 
    } 


} 

我對如何做到這一點不知道。請幫幫我。

感謝

+1

檢查,如果應用程序在後臺從[這裏](https://stackoverflow.com/questions/3667022/checking-if運行-an-android-application-is-running-in-the-background) – ELITE

+0

嘗試將'PendingIntent.FLAG_UPDATE_CURRENT'更改爲'PendingIntent.FLAG_ONE_SHOT' – ZeroOne

+0

應用程序在某種意義上是開放的,它對用戶是可見的? – Sanoop

回答

4

試試這個方法,

public static boolean isAppInForeground(Context context) { 
    List<ActivityManager.RunningTaskInfo> task = 
      ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)) 
        .getRunningTasks(1); 
    if (task.isEmpty()) { 
     // app is in background 
     return false; 
    } 
    return task 
      .get(0) 
      .topActivity 
      .getPackageName() 
      .equalsIgnoreCase(context.getPackageName()); 
} 

它返回一個布爾值,表示應用程序在背景或前景

編輯

或者你也可以通過提供做這樣包名

在包名稱中使用下面的方法。如果您的任何活動在前臺,它將返回true。

public boolean isForeground(String myPackage) { 
    ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); 
    List<ActivityManager.RunningTaskInfo> runningTaskInfo = manager.getRunningTasks(1); 
    ComponentName componentInfo = runningTaskInfo.get(0).topActivity; 
    return componentInfo.getPackageName().equals(myPackage); 
} 

添加權限:

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

希望這有助於

+0

對不起,即時通訊新的android。在我的情況下添加此代碼?謝謝。 – Joehamir

+0

將此代碼添加到任何類或應用程序類(如果您正在使用)。然後調用'className.isAppInForeground(anyActivityContextObject);' – Sanoop

+0

謝謝,我會嘗試實現這個到我的應用程序,以便通知不會顯示應用程序何時打開。即時通訊只是想弄清楚放哪裏以及何時打電話給我。對不起我的英語和作爲noob。 :D – Joehamir

相關問題