Manoj Srivatsav
Manoj Srivatsav

Reputation: 280

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?

Upvotes: 5

Views: 2176

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006964

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

Upvotes: 2

Related Questions