应用程序打开时不显示FCM通知

时间:2017-06-22 04:51:35

标签: android

我在我的Android应用程序中处理了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 个答案:

答案 0 :(得分: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" />

希望这有帮助