当应用程序打开时不显示FCM通知
问题描述:
我在我的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());
}
}
}
我对如何做到这一点不知道。请帮帮我。
感谢
答
试试这个方法,
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" />
希望这有助于
检查,如果应用程序在后台从[这里](https://stackoverflow.com/questions/3667022/checking-if运行-an-android-application-is-running-in-the-background) – ELITE
尝试将'PendingIntent.FLAG_UPDATE_CURRENT'更改为'PendingIntent.FLAG_ONE_SHOT' – ZeroOne
应用程序在某种意义上是开放的,它对用户是可见的? – Sanoop