trna ha minh
trna ha minh

Reputation: 253

Click on notification does not start activity

Click on notification does not start a specific activity My code

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

As you can see, I try to open MainScreenActivity activity when the user clicks on the notification and it does not start MainScreenActivity when clicked. If I replace MainScreenActivity with other acitivies then it works just fine.

Note that I have tried many solutions. add android:exported="true" in activity tag in manifest change flag to PendingIntent.FLAG_CANCEL_CURRENT

If I try to start other activities then it works. The problem ony happens when I try to start specifically MainScreenActivity

Upvotes: 0

Views: 70

Answers (1)

Mohd Saquib
Mohd Saquib

Reputation: 580

Replace this code

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

to

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

Upvotes: 1

Related Questions