java - 单击通知不启动 Activity

标签 java android

点击通知不会启动特定 Activity 我的代码

   NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

int notificationId = (int) System.currentTimeMillis();

    Intent picukUpIntent = new Intent(context, MainScreenActivity.class);
                    picukUpIntent.putExtra(MainScreenActivity.ORDER_ID, orderId);
                    picukUpIntent.putExtra(NOTI_TYPE, 3);
                    pendingIntent =
                            PendingIntent.getActivity(
                                    context,
                                    notificationId,
                                    picukUpIntent,
                                    PendingIntent.FLAG_ONE_SHOT

                            );

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.ic_mini_logo)
                    .setContentTitle("Title")
                    .setContentText(message)
                    .setVisibility(Notification.VISIBILITY_PUBLIC)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setAutoCancel(true);

    builder.setContentIntent(pendingIntent);

            mNotifyMgr.notify(notificationId, builder.build());

如您所见,我尝试在用户单击通知时打开 MainScreenActivity Activity ,但单击时它不会启动 MainScreenActivity。如果我用其他 Activity 替换 MainScreenActivity 那么它就可以正常工作。

请注意,我已经尝试了很多解决方案。 在 list 中的 Activity 标记中添加 android:exported="true" 将标志更改为 PendingIntent.FLAG_CANCEL_CURRENT

如果我尝试开始其他 Activity ,那么它就会起作用。仅当我尝试专门启动 MainScreenActivity 时才会出现此问题

最佳答案

替换此代码

 pendingIntent = PendingIntent.getActivity(
                                    context,
                                    notificationId,
                                    picukUpIntent,
                                    PendingIntent.FLAG_ONE_SHOT);

 pendingIntent = PendingIntent.getActivity(
                                    context,
                                    notificationId,
                                    picukUpIntent,
                                    PendingIntent.FLAG_UPDATE_CURRENT
                            | PendingIntent.FLAG_ONE_SHOT);

关于java - 单击通知不启动 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45705941/

相关文章:

android - 使用 View 绑定(bind)而不是 findViewById 有什么缺点吗?

java - Android小游戏-延迟代码

java - Eclipse - 当涉及 Maven 时为 "Run as Java Application"

java - 如何正确使用继承,避免向下转型

安卓工作室 - 错误 : No resource found that matches the given name: attr 'metaButtonBarButtonStyle'

android - 在安卓中停止观看

android - 在android的onPreviewFrame期间转换YUV-> RGB(图像处理)-> YUV?

java - 在 Java 中打包这些类的惯用方法是什么

java - 如何在同一版本的flyway中执行多个sql脚本

java - 拆分字符串类型并放入循环中的新数组中