Open application after clicking on Notification
- Reza_Rg
- 2012-12-05 05:13
- 9
I have a notification in my app with the following code:
//Notification Start notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); int icon = R.drawable.n1; CharSequence tickerText = "Call Blocker"; long when = System.currentTimeMillis(); //now Notification notification = new Notification(icon, tickerText, when); Intent notificationIntent = new Intent(context, Main.class); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); Context context = getApplicationContext(); CharSequence title = "Call Blocker"; text = "Calls will be blocked while driving"; notification.setLatestEventInfo(context, title, text, contentIntent); notification.flags |= Notification.FLAG_ONGOING_EVENT; notification.flags |= Notification.FLAG_SHOW_LIGHTS; notificationManager.notify(1, notification); }
My notifications fires very well, but my problem is that, when I click on notification in Notification Center, it does not start my app.
Basically, after clicking on my notification nothing happens! What should I do, in order to start my Main activity after clicking on my notification. Thanks.
9 Answers
Here's example using NotificationCompact.Builder class which is the recent version to build notification.
private void startNotification() { Log.i("NextActivity", "startNotification"); // Sets an ID for the notification int mNotificationId = 001; // Build Notification , setOngoing keeps the notification always in status bar NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ldb) .setContentTitle("Stop LDB") .setContentText("Click to stop LDB") .setOngoing(true); // Create pending intent, mention the Activity which needs to be //triggered when user clicks on notification(StopScript.class in this case) PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, StopScript.class), PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(contentIntent); // Gets an instance of the NotificationManager service NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); // Builds the notification and issues it. mNotificationManager.notify(mNotificationId, mBuilder.build()); }
open specific activity on notification click android, click_action is a parameter of the notification payload. If you want to open your app and perform a specific action, set click_action in the notification payload and map it to an intent filter in the Activity you want to launch. For example, set click_action to OPEN_ACTIVITY_1 to trigger an intent filter like the following: click_action is a parameter of the notification payload. If you want to open your app and perform a specific action, set click_action in the notification payload and map it to an intent filter in the Activity you want to launch. For example, set click_action to OPEN_ACTIVITY_1 to trigger an intent filter like the following:
Vins
2018-08-31 21:25
Please use below code for complete example of simple notification, in this code you can open application after clicking on Notification, it will solve your problem.
public class AndroidNotifications extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button notificationButton = (Button) findViewById(R.id.notificationButton); notificationButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { // Notification Title and Message Notification("Dipak Keshariya (Android Developer)", "This is Message from Dipak Keshariya (Android Developer)"); } }, 0); } }); } // Notification Function private void Notification(String notificationTitle, String notificationMessage) { NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); android.app.Notification notification = new android.app.Notification( R.drawable.ic_launcher, "Message from Dipak Keshariya! (Android Developer)", System.currentTimeMillis()); Intent notificationIntent = new Intent(this, AndroidNotifications.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(AndroidNotifications.this, notificationTitle, notificationMessage, pendingIntent); notificationManager.notify(10001, notification); } }
And see below link for more information.
android notification click open new activity, Try setting the flags to Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP instead.. From the documentation for FLAG_ACTIVITY_CLEAR_TOP (emphasis mine):. If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be Try setting the flags to Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP instead.. From the documentation for FLAG_ACTIVITY_CLEAR_TOP (emphasis mine):. If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be
Dipak Keshariya
2012-12-05 05:26
Looks like you missed this part,
notification.contentIntent = pendingIntent;
Try adding this and it should work.
android notification click listener, onClick() listener for notification-1. How to close an android app from notification bar. 0. Android Notification Click - don't backstack activity. Hot Network onClick() listener for notification-1. How to close an android app from notification bar. 0. Android Notification Click - don't backstack activity. Hot Network
Andro Selva
2012-12-05 05:15
Use below code for create notification for open activity. It works for me. For full code
Intent myIntent = new Intent(context, DoSomething.class); PendingIntent pendingIntent = PendingIntent.getActivity( context, 0, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK); myNotification = new NotificationCompat.Builder(context) .setContentTitle("Exercise of Notification!") .setContentText("Do Something...") .setTicker("Notification!") .setWhen(System.currentTimeMillis()) .setContentIntent(pendingIntent) .setDefaults(Notification.DEFAULT_SOUND) .setAutoCancel(true) .setSmallIcon(R.drawable.ic_launcher) .build(); notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
android fcm notification click open activity, If app in foreground then on notification click it simply dismisses notification. However, I want to receive data and open specific Activity in both scenarios (background and foreground). I have in MainActivity the following code, but I am not able to get data. fcm_notification, date, hal_id returns null. If app in foreground then on notification click it simply dismisses notification. However, I want to receive data and open specific Activity in both scenarios (background and foreground). I have in MainActivity the following code, but I am not able to get data. fcm_notification, date, hal_id returns null.
Ankit Kumar Garg
2016-12-27 16:19
use this:
Notification mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_music) .setContentTitle(songName).build(); mBuilder.contentIntent= PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
contentIntent will take care of openning activity when notification clicked
call fragment from notification click android, When you send notification this above two methods runs. So after that you will click the notification on your phone.. right.. This click operation opens the Intent(ActivityName) that you set before in sendNotification(..) method. E.g you click the notification and your ProducerActivity or EventListActivity opens.. When you send notification this above two methods runs. So after that you will click the notification on your phone.. right.. This click operation opens the Intent(ActivityName) that you set before in sendNotification(..) method. E.g you click the notification and your ProducerActivity or EventListActivity opens..
Lily Sedghi
2015-11-07 08:56
Thanks to above posts, here's the main lines - distilled from the longer code answers - that are necessary to connect a notification with click listener set to open some app Activity.
private Notification getNotification(String messageText) { Notification.Builder builder = new Notification.Builder(this); builder.setContentText(messageText); // ... Intent appActivityIntent = new Intent(this, SomeAppActivity.class); PendingIntent contentAppActivityIntent = PendingIntent.getActivity( this, // calling from Activity 0, appActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(contentAppActivityIntent); return builder.build(); }
open a specific activity from firebase notification, I integrated firebase notification to my application but I would like to send a notification that opens a specific activity and does what I schedule to do, not just opening the App. like a notification that will push the user to visit Google play store on clicking it. I integrated firebase notification to my application but I would like to send a notification that opens a specific activity and does what I schedule to do, not just opening the App. like a notification that will push the user to visit Google play store on clicking it.
Gene Bo
2018-01-26 23:47
public static void notifyUser(Activity activity, String header, String message) { NotificationManager notificationManager = (NotificationManager) activity .getSystemService(Activity.NOTIFICATION_SERVICE); Intent notificationIntent = new Intent( activity.getApplicationContext(), YourActivityToLaunch.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(activity); stackBuilder.addParentStack(YourActivityToLaunch.class); stackBuilder.addNextIntent(notificationIntent); PendingIntent pIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); Notification notification = new Notification.Builder(activity) .setContentTitle(header) .setContentText(message) .setDefaults( Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE) .setContentIntent(pIntent).setAutoCancel(true) .setSmallIcon(drawable.notification_icon).build(); notificationManager.notify(2, notification); }
android notification not opening activity, 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 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
Neo
2016-11-20 17:27
This is the way that I have approached.
public class AppFCMService extends FirebaseMessagingService { private final static String TAG = "FCM Message"; String notify, requstId, Notification; PendingIntent pendingIntent; @Override public void onMessageReceived(RemoteMessage remoteMessage) { Log.d(TAG, "From: " + remoteMessage.getFrom()); Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); //split string and getting order id from notification String Str = remoteMessage.getNotification().getBody(); String[] tmp; tmp = Str.split(" "); String temp1 = tmp[0]; String temp2 = tmp[1]; String id = tmp[2]; notify = temp1 + " " + temp2; requstId = id; showNotification(remoteMessage.getNotification().getBody()); } private void showNotification(String messageBody) { // check whether session has been initiated or not if (new SessionHelper(getApplicationContext()).isLoggedIn()) { if (notify.equalsIgnoreCase("Travel request")) { Intent intent = new Intent(this, ViewTravelDetailsActivity.class); intent.putExtra("TravelRequestID", requstId); intent.putExtra("BackPress", "Notify"); pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); } else if (notify.equalsIgnoreCase("Timesheet replied")) { Intent intent = new Intent(this, ViewReplyActivity.class); intent.putExtra("timesheetActivityID", requstId); intent.putExtra("BackPress", "Notify"); intent.putExtra("RealmData", "DeleteRealm"); intent.putExtra("isToday", "true"); pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); } else { Intent intent = new Intent(this, Dashboard.class); intent.putExtra("timesheetActivityID", requstId); pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); } } else { Intent intent = new Intent(this, LoginActivity.class); intent.putExtra("timesheetActivityID", requstId); pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); } Bitmap notifyImage = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(notifyImage) .setColor(Color.parseColor("#FFE74C3C")) .setContentTitle("TEST") .setContentText(messageBody) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, notificationBuilder.build()); } }
notification click event android, How to send data from Notification with click Notification android. 0. Launch a Notification and when tap on it, run other function. Related. 2636. How to send data from Notification with click Notification android. 0. Launch a Notification and when tap on it, run other function. Related. 2636.
arc_shiva
2018-02-10 07:12
See below code. I am using that and it is opening my HomeActivity.
Start an Activity from a Notification, See below code. I am using that and it is opening my HomeActivity. NotificationManager notificationManager = (NotificationManager) context . to use Pending Intents to open an Activity by clicking a notification. not as new activity Duration: 3:23 Posted: Oct 9, 2017
Nirali
2012-12-05 05:19