Android Call Method On Notification Click
This code creates a notification. If you click it, the current application is ran (the intent is created in Entry, which is my only Activity), a slightly modified version of a Andr
Solution 1:
Add android:launchMode="singleTop"
in your activity
in your manifest file, have the method protected void onNewIntent(Intent intent) { ... }
and use this code:
privatestaticfinalintMY_NOTIFICATION_ID=1;
private NotificationManager notificationManager;
private Notification myNotification;
voidnotification() {
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
myNotification = newNotification(R.drawable.next, "Notification!", System.currentTimeMillis());
Contextcontext= getApplicationContext();
StringnotificationTitle="Exercise of Notification!";
StringnotificationText="http://android-er.blogspot.com/";
IntentmyIntent=newIntent(this, YourActivity.class);
PendingIntentpendingIntent= PendingIntent.getActivity(YourActivity.this, 0, myIntent, Intent.FILL_IN_ACTION);
myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
myNotification.setLatestEventInfo(context, notificationTitle, notificationText, pendingIntent);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
}
Solution 2:
This worked 100% for me:
Place this code in a method:
Intentintent=newIntent(this, YourClass.class);
intent.putExtra("NotiClick",true);
PendingIntentpIntent= PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
Notification Noti;
Noti = newNotification.Builder(this)
.setContentTitle("YourTitle")
.setContentText("YourDescription")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pIntent)
.setAutoCancel(true).build();
NotificationManagernotificationManager=
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, Noti);
}
Then in the onCreate/constructor of your class do this:
if (savedInstanceState == null) {
Bundleextras= getIntent().getExtras();
if(extras == null)
{
//Cry about not being clicked on
}
elseif (extras.getBoolean("NotiClick"))
{
//Do your stuff here mate :)
}
}
Solution 3:
Intentintent=newIntent(this, Notificationintent.class);
PendingIntentpIntent= PendingIntent.getActivity(this, 0, intent, 0);
Notificationnoti=newNotification.Builder(this)
.setContentTitle("APP NOTIFICATION")
.setContentText(messageValue)
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(newNotification.BigTextStyle()
.bigText(messageValue))
.setContentIntent(pIntent).build();
NotificationManagernotificationManager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
Post a Comment for "Android Call Method On Notification Click"