Clicking on Notification is not starting intended activity?
Solution 1
My problem got solved I just have to add PendingIntent.FLAG_ONE_SHOT
flag as well , so I replaced :
PendingIntent contentIntent = stackBuilder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
to
PendingIntent contentIntent = stackBuilder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
Solution 2
I encountered the same issue and resolved it by adding android:exported="true" to the activity declaration in AndroidManifest.xml.
Solution 3
Here you just passed your Intent into pendingintent: see below
Intent notificationIntent = new Intent(context, Login.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
and set this contentintent into your Notification:
Notification noti = new NotificationCompat.Builder(context)
.setSmallIcon(icon_small)
.setTicker(message)
.setLargeIcon(largeIcon)
.setWhen(System.currentTimeMillis())
.setContentTitle(title)
.setContentText(message)
.setContentIntent(**contentIntent**)
.setAutoCancel(true).build();
This may help you.
Ansh
Updated on July 22, 2020Comments
-
Ansh almost 4 years
I am using GCM in my application and also using NotificationManager to Create a Notification whenever GCM message is received.Till now everything is working perfectly and GCM message is showing correctly in Notification area, but when I click on the notification it should start an activity of my application which will display the message detail which is not happening. Every-time I click on notification it does not start any activity and it remains as is.My code for creating Notification is :
private void sendNotification(String msg) { SharedPreferences prefs = getSharedPreferences( DataAccessServer.PREFS_NAME, MODE_PRIVATE); mNotificationManager = (NotificationManager) this .getSystemService(Context.NOTIFICATION_SERVICE); Intent intent = new Intent(this, WarningDetails.class); Bundle bundle = new Bundle(); bundle.putString("warning", msg); bundle.putInt("warningId", NOTIFICATION_ID); intent.putExtras(bundle); // The stack builder object will contain an artificial back stack for // the // started Activity. // This ensures that navigating backward from the Activity leads out of // your application to the Home screen. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); // Adds the back stack for the Intent (but not the Intent itself) stackBuilder.addParentStack(WarningDetails.class); // Adds the Intent that starts the Activity to the top of the stack stackBuilder.addNextIntent(intent); PendingIntent contentIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( this).setSmallIcon(R.drawable.weather_alert_notification) .setContentTitle("Weather Notification") .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) .setContentText(msg); String selectedSound = prefs.getString("selectedSound", ""); if (!selectedSound.equals("")) { Uri alarmSound = Uri.parse(selectedSound); mBuilder.setSound(alarmSound); } else { Uri alarmSound = RingtoneManager .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); mBuilder.setSound(alarmSound); } if (prefs.getBoolean("isVibrateOn", false)) { long[] pattern = { 500, 500, 500, 500, 500, 500, 500, 500, 500 }; mBuilder.setVibrate(pattern); } mBuilder.setContentIntent(contentIntent); mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); }
I updated my code to support
Preserving Navigation when Starting an Activity
just like it happens in Gmail application using the Android developers website since then it stopped working.Someone Please guide me what I am missing or doing wrong in this code. -
Ansh over 10 yearsBut The method getNotification() from the type NotificationCompat.Builder is deprecated
-
Ansh over 10 yearsYour code does not support Preserving Navigation when Starting an Activity
-
Ansh over 10 yearsWill this supports Preserving Navigation when Starting an Activity like Gmail application?
-
AndroidHacker over 10 yearsDid you used my code in generateNotification() method. If yes then what error you are getting ?
-
Ansh over 10 yearsProblem is it's not preserving the BackStack after starting Activity
-
AndroidHacker over 10 yearsSorry I am not able to get your problem clearly. You mean to say that now on clicking notification activity is started and when we back trace from their then app crashes or some thing like that
-
Ansh over 10 years@Saty I used it in sendNotification() method which is posted in my question
-
Saty over 10 yearsHi you used TaskStackBuilder which needs me to require API 16 as minimum need which i dont want to do...is there any alternatives?
-
Ansh over 10 years@Saty TaskStackBuilder is present in android support library which is android.support.v4.app.TaskStackBuilder. So you can use it for API 7 as well by just using support library.
-
Saty over 10 yearshow would i able to find the message in my MainActivity which is in your case is Warning?
-
Ansh over 10 years@Saty what do you mean by find the message in your MainActivity?
-
Saty over 10 yearsif I will set the values in intent how would i be able to retrieve cause my onCreate is not called.
-
jonathanrz almost 10 yearsThank you, you saved my day.
-
cwhsu over 9 yearsThanks a lot, save me some time!
-
phil about 9 yearsthis solved my problem too. Before nothing happens. With adding that line into the manifest it works:-) Thanks!
-
supermus almost 9 yearsthis was the solution for me too. But I wonder 1 month it was working without FLAG_ON_SHOT part and just stopped working
-
Wooseong Kim over 8 yearsThanks! you saved my day!
-
sud007 over 7 yearsWorked for me as well! Superb!
-
Neon Warge about 7 yearsCan you please explain why this works? It appears this answer is just a guess. Prove me wrong.
-
Radhey over 6 yearsStrange! apparently work for me. can you describe it what is the logic behind this?
-
NixSam over 6 yearsPendingIntent.FLAG_ONE_SHOT will open it once, next time you press notification, it won't do anything. developer.android.com/reference/android/app/…
-
rahulrvp over 4 yearsThis worked for me too. The logic behind this (which I believe) is that if the activity has to be called from a different application, you should make it an exported activity. As the notification click is happening outside the app, I think we should declare the activity as exported. Reference developer.android.com/guide/topics/manifest/…