点击通知无法启动活动

时间:2017-08-16 05:50:30

标签: java android

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

   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活动,并在单击时不启动MainScreenActivity。如果我用其他活动替换MainScreenActivity,那么它可以正常工作。

请注意,我尝试了很多解决方案。 在manifest中的activity标记中添加android:exported =“true” 将标志更改为PendingIntent.FLAG_CANCEL_CURRENT

如果我尝试开始其他活动,那么它可行。当我尝试专门启动MainScreenActivity

时会发生问题

1 个答案:

答案 0 :(得分:1)

替换此代码

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

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