Notification Not Open Activity Onclick
I want to create a notification which should open activity when click on it. But when I click on notification activity not open . Here is my code: NotificationManager notification
Solution 1:
Use this:
Notification.Buildernotification=newNotification.Builder(context)
.setContentIntent(getDialogPendingIntent(Text, intentName));
private PendingIntent getDialogPendingIntent(String dialogText,
String intentname) {
return PendingIntent.getActivity(
context,
dialogText.hashCode(),
newIntent(ACTION_DIALOG)
.putExtra(Intent.EXTRA_TEXT, dialogText)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.setAction(intentname), 0);
}
getDialogPendingIntent(Text, intentName) : intentName=com.yourProject.exrta.yourIntentName
You can Change addFlags
or putExtra
if you want.
If Call with Intent Name doesn't work use it with class like that and it must work :
IntentnotificationIntent=newIntent(MainActivity.this, TestActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntentintent= PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, 0);
NotificationManagernotificationManager= (NotificationManager) MainActivity.this
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Buildernotification=newNotification.Builder(MainActivity.this)
.setContentTitle("Message Received")
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setContentIntent(intent);
Notificationnotificationn= notification.getNotification();
notificationManager.notify(1, notificationn);
Solution 2:
Try this:
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = newIntent(context, MessageReceivedActivity.class);
intent.putExtra("payload", payload);
intent.setAction(Long.toString(System.currentTimeMillis()));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification.Builder notification = new Notification.Builder(context)
.setContentTitle("Message Received")
.setSmallIcon(R.drawable.icon)
.setContentText(payload)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
Notification notificationn = notification.build();
notificationManager.notify(YOUR_NOTIF_ID, notificationn);
Solution 3:
This worked for me:
Contextcontext1= loggedin.this.getApplicationContext();
NotificationManagernotificationManager= (NotificationManager)context1.getSystemService(NOTIFICATION_SERVICE);
NotificationupdateComplete=newNotification();
updateComplete.icon = android.R.drawable.stat_notify_sync;
updateComplete.tickerText = context1.getText(R.string.notification_title);
updateComplete.when = System.currentTimeMillis();
IntentnotificationIntent=newIntent(context1,loggedin.class);
PendingIntentcontentIntent= PendingIntent.getActivity(context1, 0,notificationIntent, 0);
StringcontentTitle= context1.getText(R.string.notification_title).toString();
String contentText;
contentText = context1.getText(R.string.notification_info_success).toString();
updateComplete.setLatestEventInfo(context1, contentTitle,contentText, contentIntent);
intLIST_UPDATE_NOTIFICATION=0;
notificationManager.notify(LIST_UPDATE_NOTIFICATION, updateComplete);
Post a Comment for "Notification Not Open Activity Onclick"