Perform An Action Clicking Notifications In Android
I am trying to create notifications correctly, but as I can not perform an action when you click on the notification. I hope you can help me or give some clue.
Solution 1:
It is recommended to use Notification.Builder
and NotificationCompat.Builder
from the support library for building notifications.
Also why do use reuse the Intent
which was caught by your BroadcastReceiver
? It should refer to the actual activity which you want to launch by click.
For example:
Intentintent=newIntent(context, MainActivity.class);
Notificationnotification=newNotificationCompat.Builder(context)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setContentIntent(PendingIntent.getActivity(context, 0, intent, 0))
.setAutoCancel(true)
.build();
If you doesn't really want to launch some activity, you may start a Service
or send a broadcast message instead. See PendingIntent.
Solution 2:
NotificationManagernm= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notificationnotif=newNotification(R.drawable.ic_launcher, "Text",
System.currentTimeMillis());
Intentintent=newIntent(this, MainActivity.class);
intent.putExtra("somekey", "someextra");
PendingIntentpIntent= PendingIntent.getActivity(this, 0, intent, 0);
notif.setLatestEventInfo(this, "Title", "Message", pIntent);
notif.flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(1, notif);
Use Notification.Builder for new version api
Post a Comment for "Perform An Action Clicking Notifications In Android"