Android 如果在应用程序处于后台时单击firebase通知,则应用程序将重新加载
当应用程序运行时,如果我单击它,通知将发出警报,通知将转到那里(Android 如果在应用程序处于后台时单击firebase通知,则应用程序将重新加载,android,firebase,notifications,firebase-notifications,Android,Firebase,Notifications,Firebase Notifications,当应用程序运行时,如果我单击它,通知将发出警报,通知将转到那里(onMessageReceived正在运行) 但当应用程序位于后台,并且如果我单击它,通知将发出警报时,应用程序将再次重新加载(最初如启动屏幕),而不是转到那里,并且onMessageReceived未运行 如何修复它 ps.对不起,我的英语不好。您需要将所需的活动作为intent传递,而不是主活动将活动名称指定给待定intent Intent intent = new Intent(this, Notification
onMessageReceived
正在运行)
但当应用程序位于后台,并且如果我单击它,通知将发出警报时,应用程序将再次重新加载(最初如启动屏幕),而不是转到那里,并且onMessageReceived
未运行
如何修复它
ps.对不起,我的英语不好。您需要将所需的活动作为intent传递,而不是主活动将活动名称指定给待定intent
Intent intent = new Intent(this, NotificationCheck.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("App Name")
.setContentText(messageBody)
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
请参阅下面的代码。我正在使用它,它开启了我的家庭活动
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
//change HomeActivity.class which activity you want to open after clicking notification
Intent notificationIntent = new Intent(context, HomeActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
更改HomeActivity.class单击通知后要打开的活动。。希望它能起作用
/**
* Method checks if the app is in background or not
*/
public static boolean isAppIsInBackground(Context context) {
boolean isInBackground = true;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
for (String activeProcess : processInfo.pkgList) {
if (activeProcess.equals(context.getPackageName())) {
isInBackground = false;
}
}
}
}
} else {
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
if (componentInfo.getPackageName().equals(context.getPackageName())) {
isInBackground = false;
}
}
return isInBackground;
}
我希望它能帮助您。Intent-Intent=新的Intent(context,TargetActivity.class);我的应用程序当应用程序在后台时,如果我单击它,通知将发出警报,onMessageReceived未运行。请将代码放在此处。。没有代码的bocaz我们无法理解这个问题。要推送方法的类在哪里
isAppIsInBackground
将此方法放在NotificationUtils类中,然后您可以在MyFirebaseMessagingService类中使用它。
private void handleNotification(String message) {
if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
// app is in foreground, broadcast the push message
Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
pushNotification.putExtra("message", message);
LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
// play notification sound
NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
notificationUtils.playNotificationSound();
}else{
// If the app is in background, firebase itself handles the notification
}
}