gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:38:56 25 4
gpt4 key购买 nike

我的 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());
}
}


}

我不知道该怎么做。请帮助我。

谢谢

最佳答案

试试这个方法,

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

它返回一个 bool 值,指示应用程序是在后台还是在前台

编辑

或者你可以通过提供包名来做到这一点

将以下方法与您的包名称一起使用。如果您的任何 Activity 在前台,它将返回 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" />

希望对你有帮助

关于android - 应用程序打开时不显示 FCM 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44690398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com