当用户向下拖动所有通知然后点击我的应用通知时,应用将打开。
但是,如果新通知出现在设备屏幕的顶部并且用户点击它,则它不会打开。
可能是什么问题?
Intent intent = Activity1.createIntent(app.getApplicationContext());
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Activity2.createIntent(app.getApplicationContext(), new ArrayList<>(participants));
PendingIntent pendingIntent =
PendingIntent.getActivity(app.getApplicationContext(), 1000, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Bitmap bm = BitmapFactory.decodeResource(app.getResources(), R.mipmap.appiconmain);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(app.getApplicationContext())
.setSmallIcon(R.drawable.new_item_arrival, Notification.PRIORITY_HIGH)
.setLargeIcon(bm)
.setContentTitle(displayNotificationData.getTitle())
.setContentText(displayNotificationData.getSubTitle())
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setPriority(Notification.PRIORITY_HIGH)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
最佳答案
您需要查看覆盖Activity
的onNewIntent(Intent intent)
事件处理程序,在此方法上您应该能够执行您需要的任何任务
此外,根据 official docs你仍然可以通过 getIntent()
获得原始 Intent ,因此仍然在 onNewIntent()
中你应该考虑使用 setIntent (Intent newIntent)
所以你可以在 Activity 的任何地方使用这个新的 Intent
Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.
看看this引用一个精确而简洁的答案:
For managing if the activity is already running you have two ways:
Add FLAG_ACTIVITY_SINGLE_TOP flag to the Intent when launching the activity, and then in the activity class implement onNewIntent(Intent intent) event handler, that way you can access the new intent that was called for the activity (which is not the same as just calling getIntent(), this will always return the first Intent that launched your activity.
Same as number one, but instead of adding a flag to the Intent you must add "singleTop" in your activity AndroidManifest.xml.
If you use intent extras, remeber to call PendingIntent.getActivity() with the flag PendingIntent.FLAG_UPDATE_CURRENT, otherwise the same extras will be reused for every notification.
希望对你有帮助
关于Android 应用程序不会在点击设备屏幕顶部出现的通知时打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40076988/