Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send an ACTION_SEND intent from notification

I am trying to send and Intent.ACTION_SEND on click of a notification. This is what I have done

public static void generateNotification(Context context, String title, String message)
{ 
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Builder notificationBuilder = new Notification.Builder(context)
                                        .setContentTitle(title)
                                        .setContentText(message)
                                        .setSmallIcon(R.drawable.ic_launcher)
                                        .setWhen(System.currentTimeMillis());

    Intent shareIntent = new Intent(Intent.ACTION_SEND);    
    String extraText = title + " has " + message;
    shareIntent.putExtra(Intent.EXTRA_TEXT, extraText);
    PendingIntent pendingShareIntent = PendingIntent.getActivity(context, 0, Intent.createChooser(shareIntent, "share..."),
                                                                PendingIntent.FLAG_UPDATE_CURRENT);

    notificationBuilder.addAction(android.R.drawable.ic_menu_share, "share...", pendingShareIntent);

    Notification bigTextNotification = new Notification.BigTextStyle(notificationBuilder)
                                        .setBigContentTitle(title)
                                        .bigText(message)
                                        .build();

    notificationManager.notify(tag, notificationId++, bigTextNotification);
}

I get the share action on the notification but when i click it i a dialog box that says

No Apps can perform this action

When I run the same intent via startActivity() it works fine.

Can somebody help me out here?

like image 737
Manoj Srivatsav Avatar asked Nov 01 '22 15:11

Manoj Srivatsav


1 Answers

You failed to specify a MIME type on the Intent, using setType(). Try adding that and see if it helps.

like image 135
CommonsWare Avatar answered Nov 12 '22 18:11

CommonsWare