android - Launch Homescreen on Notification Click -
here notification builder :
public void customnotification() { // using remoteviews bind custom layouts notification remoteviews remoteviews = new remoteviews(getpackagename(), r.layout.customnotification); // set notification title string strtitle = getstring(r.string.customnotificationtitle); // set notification text string strtext = getstring(r.string.customnotificationtext); intent intent = new intent(this, main2activity.class); // send data notificationview class intent.putextra("title", strtitle); intent.putextra("text", strtext); pendingintent pintent = pendingintent.getactivity(this, 0, intent, pendingintent.flag_update_current); notificationcompat.builder builder = new notificationcompat.builder(this) // set icon .setsmallicon(r.mipmap.ic_launcher) // set ticker message .setticker(getstring(r.string.customnotificationticker)) // dismiss notification .setongoing(true) // set pendingintent notification .setcontentintent(pintent) // set remoteviews notification .setcontent(remoteviews); // locate , set images remoteviews.setimageviewresource(r.id.imagenotileft, r.mipmap.ic_launcher); remoteviews.setimageviewresource(r.id.imagenotiright, r.mipmap.ic_launcher); remoteviews.settextviewtext(r.id.title, getstring(r.string.customnotificationtitle)); remoteviews.settextviewtext(r.id.text, getstring(r.string.customnotificationtext)); // create notification manager notificationmanager notificationmanager = (notificationmanager) getsystemservice(notification_service); // build notification notification manager notificationmanager.notify(0, builder.build()); }
now question :
is possible launch homescreen (device home
) when notification clicked (or user taken homescreen when notification clicked) ? if yes, how?
hey use code solve issue mention activity in pendingintent.
private static void generatenotification(context context, string message) { int icon = r.drawable.ic_launcher; long when = system.currenttimemillis(); notificationmanager notificationmanager = (notificationmanager) context.getsystemservice(context.notification_service); notification notification = new notification(icon, message, when); string title = context.getstring(r.string.app_name); intent notificationintent = new intent(context, mainactivity.class); // set intent not start new activity notificationintent.setflags(intent.flag_activity_clear_top | intent.flag_activity_single_top); pendingintent intent = pendingintent.getactivity(context, 0, notificationintent, 0); notification.setlatesteventinfo(context, title, message, intent); notification.flags |= notification.flag_auto_cancel; // play default notification sound notification.defaults |= notification.default_sound; //notification.sound = uri.parse( "android.resource://" + context.getpackagename() + "your_sound_file_name.mp3"); // vibrate if vibrate enabled notification.defaults |= notification.default_vibrate; notificationmanager.notify(0, notification); }
here complete tutorial http://androidexample.com/android_push_notifications_using_google_cloud_messaging_gcm/index.php?view=article_discription&aid=119&aaid=139
Comments
Post a Comment