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

user1796624
  • 3,665
  • 7
  • 38
  • 65

5 Answers5

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>
Zala Janaksinh
  • 2,929
  • 5
  • 32
  • 58
user755
  • 2,521
  • 3
  • 18
  • 29
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

Community
  • 1
  • 1
user1796624
  • 3,665
  • 7
  • 38
  • 65
0

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

Malcolm
  • 41,014
  • 11
  • 68
  • 91
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();
Andy Res
  • 15,963
  • 5
  • 60
  • 96
  • sorry but this is not working, it makes new instance from the activity – user1796624 Jul 09 '13 at 13:06
  • Then you may just change PendingIntent.getActivity() to PendingIntent.getBroadcast(), and make your IntentReceiver to receive this intent, and do whatever you want. – Mine Jul 09 '13 at 13:16
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!

SchmitzIT
  • 9,227
  • 9
  • 65
  • 92
imthegiga
  • 1,126
  • 8
  • 13
  • 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 – user1796624 Jul 09 '13 at 13:22