Android 应用程序不会在点击设备屏幕顶部出现的通知时打开

标签 android android-intent

当用户向下拖动所有通知然后点击我的应用通知时,应用将打开。

但是,如果新通知出现在设备屏幕的顶部并且用户点击它,则它不会打开。

可能是什么问题?

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

最佳答案

您需要查看覆盖ActivityonNewIntent(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/

相关文章:

android - sdk版本改成4.4后无法用上一个android studio构建apk

android - 仅当 Firebase 任务完成时才完成 AsyncTask

php - 发送到服务器时,应用程序不会以 Release模式安装

android-intent - 通过 WebView 从图库和相机中选择图像在 Android 6.0 Marshmallow 中不起作用

android - Google Play 音乐不再支持 media_play_from_search

android - 如何在Android中获取相应的文件图标?

java - TextView 中的文本在滚动时淡出

android - 如何使用 Intent anchor 在android中使用额外数据(参数)启动我的应用程序

android - 如何在步行模式下打开谷歌地图

android - 如何从 ContactsContract.Contacts 获取联系人的 LOOKUP_KEY?