I'm working whit push notifications. When a user clicks on a notification I want to start the application in case it is not started, or bring it to front in case it is started. Thanks
5 Answers
this is the complite code I found the answer here Bring application to front after user clicks on home button
Intent intent = new Intent(ctx, SplashScreen.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT), PendingIntent.FLAG_CANCEL_CURRENT); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( ctx).setContentTitle(extras.getString("title")) .setContentText(extras.getString("message")) .setSmallIcon(R.drawable.ic_launcher) .setContentIntent(contentIntent); Notification noti = mBuilder.build(); noti.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL; mNotificationManager.notify(NOTIFICATION_ID, noti); The important things are the flags on the Intent, this will open the app or bring it to front if is opened, or do nothing if you click on the notification while you are browsing the app
Comments
implement pending intent.
Code
Intent pi = new Intent(); pi.setClass(getApplicationContext(), youactivity.class); // The PendingIntent to launch our activity if the user selects this notification PendingIntent contentIntent = PendingIntent.getActivity(this, 0,pi, PendingIntent.FLAG_UPDATE_CURRENT); String msgText = mMessage; // construct the Notification object. Notification notif = new Notification(R.drawable.icon, msgText,System.currentTimeMillis()); manifest
<activity android:name="com.InfoActivity" android:noHistory="false android:excludeFromRecents="false"></activity> Comments
Comments
You do that by creating a PendingIntent.
For example, this will launch a MainActivity when notification is clicked.
Intent intent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); Notification noti = new Notification.Builder(this) .setContentTitle("Notication title") .setContentText("Content text") .setContentIntent(pendingIntent).build(); 2 Comments
I don't know whether you're talking about Google Cloud Messaging or not. But if it is a normal Notification then while creating your notification you've to provide Pending Intent. Just put your desired class in Pending intent so that when user clicks on that notification you'll be driven to your so called Application. A snippet :
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, "Your application activity".class), PendingIntent."Flag you want"); After this use this intent while creating notification. It's method is setContentIntent("above intent") and fire your notification with NotificationManager!