我正在使用Firebase通知服务。当我从Firebase发送通知时。弹出窗口必须仅在Android中触发应用程序前台
我不想保存这些信息。我的意思是通知仅在用户在MainActivity中联机时显示。如果用户运行另一个活动,他看不到任何通知,但是当用户来到MainActiviy通知时必须到来。
public class NotificationService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String getBody = remoteMessage.getNotification().getBody();
// Uygulama suan kullanılıyorsa.
if (isAppForground(getApplicationContext())){
getApplicationContext().startActivity(new Intent(getApplicationContext(), MainActivity.class)
.putExtra("data",getBody)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}else{
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("Fixtown")
.setContentText(getBody)
.setSmallIcon(R.drawable.ic_launcher)
.build();
NotificationManagerCompat manager = NotificationManagerCompat.from(getApplicationContext());
manager.notify(123, notification);
}
}
public boolean isAppForground(Context mContext) {
ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(mContext.getPackageName())) {
return false;
}
}
return true;
}
如你所见。那么哪个标志必须放入.setFlags()以及如何仅在MainActivity中捕获此通知。
谢谢诺埃尔先生 – TeyteyLan