Intent intent = new Intent(this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent contentIntent = PendingIntent.getActivity(this, new Random().nextInt(), intent, 0);
我认为你应该添加一些逻辑来使它工作,也许这可以帮助:
的 例如,我有一个APP的启动画面(启动器和MAIN): 强>
public class SplashScreen extends AppCompatActivity { private final int TIME_OUT = 2000; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash_screen); // Suscribirse al tema Notificaciones FirebaseMessaging.getInstance().subscribeToTopic("NOTA"); if (getIntent().getExtras() != null) { if (getIntent().getExtras().size()>1){ Intent home_activity = new Intent(getApplicationContext(), Home.class); home_activity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); if (getIntent().getExtras() != null) { for (String key : getIntent().getExtras().keySet()) { String value = "" + getIntent().getExtras().getString(key); Log.d("TAG", key + "=" + value); switch (key) { case "url": home_activity.putExtra("url", value); break; } } } startActivity(home_activity); finish(); }else{ new Handler().postDelayed(new Runnable() { @Override public void run() { try { Intent home_activity = new Intent(getApplicationContext(), Home.class); home_activity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(home_activity); finish(); } catch (Exception ex) { ex.printStackTrace(); } } }, TIME_OUT); } } else { new Handler().postDelayed(new Runnable() { @Override public void run() { try { Intent home_activity = new Intent(getApplicationContext(), Home.class); home_activity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(home_activity); finish(); } catch (Exception ex) { ex.printStackTrace(); } } }, TIME_OUT); } } }
的 在我的FirebaseService中,我完成了以下操作: 强>
public class FCMessagingService extends FirebaseMessagingService { private final String TAG = "PUSH"; private String body = ""; private static String _url = ""; private static int numMessage = 0; @Override public void onMessageReceived(RemoteMessage remoteMessage) { super.onMessageReceived(remoteMessage); String from = remoteMessage.getFrom(); Log.d(TAG, "Mensaje recibido de: " + from); if (remoteMessage.getNotification() != null) { Log.d(TAG, "Notificaci锟斤拷n: " + remoteMessage.getNotification().getBody()); if (remoteMessage.getData().size() > 0) { Log.d(TAG, "Data: " + remoteMessage.getData()); try { JSONObject data = new JSONObject(remoteMessage.getData()); String url = data.getString("url"); Log.d(TAG, "onMessageReceived: \n" + "Extra Information: " + url); this._url = url; Log.d("_URL",_url); mostrarNotificacion(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody()); mensaje(url, remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody()); } catch (JSONException e) { e.printStackTrace(); } } } } private void mensaje(String url, String title, String body){ boolean acti = Util.comprobarActivityALaVista(getApplicationContext(), "com.dev.android.subagan.MainActivity"); if(acti){ Intent imain = new Intent(MainActivity.URL); imain.putExtra("key_url",url); imain.putExtra("key_title",title); imain.putExtra("key_body",body); LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(imain); }else{ Intent ihome = new Intent(Home.URL); ihome.putExtra("key_url",url); ihome.putExtra("key_title",title); ihome.putExtra("key_body",body); LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(ihome); } } private void mostrarNotificacion(String title, String body) { final int NOTIFICATION_ID = 3000; Intent intent = new Intent(this, MainActivity.class); intent.putExtra("url",_url); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT ); Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(title) .setContentText(body) .setAutoCancel(true) .setSound(soundUri) .setTicker(body) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, notificationBuilder.build()); } }
你需要设置 launchMode 的属性 Activity 你开始了 singleTop 。这将导致传入的Intent传递到现有实例,而不是在此时启动新实例 Activity 已经位于任务堆栈的顶部。
launchMode
Activity
singleTop
这是通过添加在清单中完成的 android:launchMode="singleTop" 到了 <activity> 元件。要访问最新的Intent(如果您对可能已经传入的任何数据感兴趣),请覆盖 onNewIntent() 在你的 Activity 。
android:launchMode="singleTop"
<activity>
onNewIntent()
使用 onNewIntent() 处理来自通知的新数据单击并刷新活动。
在 onNewIntent 从新意图(由新通知提供)获取新数据并捕获它们,例如:
onNewIntent
title = intent.getStringExtra("title")
在 onCreate 以前:)
onCreate
它将使用新的通知数据刷新当前活动。
你也可以关注 本教程。
Notification.Builder mBuilder = new Notification.Builder(this) .setSmallIcon(R.drawable.cmplayer) .setContentTitle("CoderoMusicPlayer") .setContentText("PLayer0!"); Intent resultIntent = new Intent(this, AndroidBuildingMusicPlayerActivity.class); resultIntent.setAction(Intent.ACTION_MAIN); resultIntent.addCategory(Intent.CATEGORY_LAUNCHER); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0); mBuilder.setContentIntent(pendingIntent); NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); mNotificationManager.notify(1, mBuilder.build());
只需复制代码并将其粘贴到主启动器活动中即可。
这是原始答案
尝试将标志设置为 Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP 代替。
Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP
来自 FLAG_ACTIVITY_CLEAR_TOP的文档 (强调我的):
的 如果设置,则正在启动的活动已在运行中 当前任务,然后而不是启动它的新实例 活动,其上的所有其他活动将被关闭 这个意图将作为一个传递到(现在在顶部)旧活动 新的意图。 强> 例如,考虑一个由活动组成的任务:A,B,C,D。 如果D调用带有解析为的Intent的startActivity() 活动B的组成部分,然后C和D将完成,B接收 给定的Intent,导致堆栈现在为:A,B。 上例中活动B的当前运行实例将是 要么接收你从这里开始的新意图 onNewIntent()方法,或者自己完成并重新使用new 意图。如果它已宣布其启动模式为“多个”( 默认情况下,您没有设置FLAG_ACTIVITY_SINGLE_TOP 意图,然后它将完成并重新创建;所有其他发射 模式或 的 如果设置了FLAG_ACTIVITY_SINGLE_TOP,则此Intent将为 传递给当前实例的onNewIntent()。 强>
的 如果设置,则正在启动的活动已在运行中 当前任务,然后而不是启动它的新实例 活动,其上的所有其他活动将被关闭 这个意图将作为一个传递到(现在在顶部)旧活动 新的意图。 强>
例如,考虑一个由活动组成的任务:A,B,C,D。 如果D调用带有解析为的Intent的startActivity() 活动B的组成部分,然后C和D将完成,B接收 给定的Intent,导致堆栈现在为:A,B。
上例中活动B的当前运行实例将是 要么接收你从这里开始的新意图 onNewIntent()方法,或者自己完成并重新使用new 意图。如果它已宣布其启动模式为“多个”( 默认情况下,您没有设置FLAG_ACTIVITY_SINGLE_TOP 意图,然后它将完成并重新创建;所有其他发射 模式或 的 如果设置了FLAG_ACTIVITY_SINGLE_TOP,则此Intent将为 传递给当前实例的onNewIntent()。 强>