Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Launch an external app from notification

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);
like image 385
corinnaerin Avatar asked Oct 14 '25 13:10

corinnaerin


1 Answers

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.

like image 134
user1137313 Avatar answered Oct 17 '25 02:10

user1137313