2

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 5

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

0

Just set the intent for the notification, this is covered in details in the official API guide.

Comments

0

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

sorry but this is not working, it makes new instance from the activity
Then you may just change PendingIntent.getActivity() to PendingIntent.getBroadcast(), and make your IntentReceiver to receive this intent, and do whatever you want.
0

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!

1 Comment

the folowing flagns need to be added intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); to the launcher intent in order to start the aplication, and

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.