我在我的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());
}
}
}
我對如何做到這一點不知道。請幫幫我。
感謝
檢查,如果應用程序在後臺從[這裏](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