4

I am trying to create a notification that when clicked will open an external app. I've seen the documentation for creating notifications and for sending the user to another app. But I can't seem to figure out how to combine the two. The problem is that the advised way to launch an app from a notification, is to creating the pending intent like this:

Intent intent = new Intent(this, MyActivity.class);
TaskStackBuilder stackBuidler = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MyActivity.class); 
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

But to launch an external app, you have to create an implicit intent like this:

String uri = ...
Intent intent = new Intent(Intent.ACTION_VIEW, uri);

As far as I can tell, there is no way to create the TaskStackBuilder with this kind of intent, because addParentStack() will only take an Activity, a Class, or a ComponentName.

I guess the question boils down to... is it possible to create a intent that is both pending and implicit?

The only workaround I can think of right now is to create an Activity in my app that does nothing but launch the external app.

I did try creating the intent from the URI then doing the following, but nothing happens when you click the notification:

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
corinnaerin
  • 386
  • 6
  • 14

3 Answers3

7

Well, a lot later, I think I have the solution for you. For all the other guys who are searching for an answer to launching an external app from your own custom notification. Here it is:

public void createMyNotification(String titl, String titlcont, String conti){
        Context context = getApplicationContext();
        PackageManager pm = context.getPackageManager();
        Intent LaunchIntent = null;
        String apppack = "com.mycompany.appack.apname.app";
        String name = "";
        try {
            if (pm != null) {
                ApplicationInfo app = context.getPackageManager().getApplicationInfo(apppack, 0);
                name = (String) pm.getApplicationLabel(app);
                LaunchIntent = pm.getLaunchIntentForPackage(apppack);
            }
            Toast.makeText(context,"Found it:" + name,Toast.LENGTH_SHORT).show();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        Intent intent = LaunchIntent; // new Intent();
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
        Notification noti = new Notification.Builder(this)
                .setTicker(titl)
                .setContentTitle(titlcont)
                .setContentText(conti)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(pIntent).getNotification();
        noti.flags = Notification.FLAG_AUTO_CANCEL;
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0, noti);
    }

This does not use an additional Activity to launch the external app.

user1137313
  • 2,390
  • 9
  • 44
  • 91
0

If you know packageName what you want start it, You can get Intent for startActivity.
Read this link start application knowing package name

Community
  • 1
  • 1
Amadas
  • 703
  • 1
  • 5
  • 10
  • That doesn't answer my question. I already know how to create a launch intent to open another app, what I don't know how to do is convert it to a PendingIntent so it can be attached to a notification. The examples in the documentation use a TaskStackBuilder, but that only works with internal Activities. – corinnaerin Mar 11 '14 at 16:58
0

I don't know if this is the best approach, but this is the solution that ended up working:

Create the notification

//Create the pending intent
Intent intent = new Intent(context, MyActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MyActivity.class);
stackBuilder.addNextIntent(intent);
PendingIndent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

//Create the notification builder
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
    .setContentTitle("Notification Title")
    .setContentText("Hello world!")
    .setContentIntent(pendingIntent); //Attach the pending intent to launch when notification is clicked

//Send the notification
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mId, builder.build());

MyActivity.java

public class MyActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Create implicit intent - see http://developer.android.com/training/basics/intents/sending.html
        String uri = "...";
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);

        startActivity(intent);
    }
}

I'd still be interested to know if there's a way to bypass having an additional Activity that does nothing but launch an implicit intent.

corinnaerin
  • 386
  • 6
  • 14