9

I have this code to make the actions:

Intent playIntent = new Intent(Intent.ACTION_VIEW);
playIntent.setDataAndType(uri, path.contains(".jpg") ? "image/jpeg" : "video/mp4");
PendingIntent play = PendingIntent.getActivity(context, 1, playIntent, 0);
mBuilder.addAction(R.mipmap.ic_play_arrow_black_48dp, "", play);

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType(path.contains(".jpg") ? "image/jpeg" : "video/mp4");
PendingIntent share = PendingIntent.getActivity(context, 2, shareIntent, 0);
mBuilder.addAction(R.mipmap.ic_share_white_48dp, "", share);

Intent doneIntent = new Intent(context, NotificationCloser.class);
doneIntent.putExtra("notificationId", notificationId);
PendingIntent done = PendingIntent.getBroadcast(context, 3, doneIntent, 0);
mBuilder.addAction(R.mipmap.ic_done_black_48dp, "", done);

And this is my broadcast receiver

public class NotificationCloser extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        int id = intent.getIntExtra("notificationId", 0);
        Log.i("MyInfo", "NotificationCloser.onReceive(" + id + ")");
        MainActivity.mNotifyManager.cancel(id);
    }
}

When I click in play or share button, it does according the function, opens the default app to view images or videos but doesn't close the notification. When I click in the done button, ONLY in the first time i receive the id correctly, after the first time it gets the id sent in the first time

Can someone help me?

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Rodrigo Butzke
  • 448
  • 4
  • 15

1 Answers1

0

I solved the problem of the broadcast by using random request codes, also added the flag PendingIntent.FLAG_UPDATE_CURRENT. I used in all because sometimes the share bugged and used another image.

PendingIntent play = PendingIntent.getActivity(context, new Random().nextInt(150) * 37, playIntent, 0);
PendingIntent share = PendingIntent.getActivity(context, new Random().nextInt(150) * 37, shareIntent, 0);
PendingIntent done = PendingIntent.getBroadcast(context, new Random().nextInt(150) * 37, doneIntent, PendingIntent.FLAG_UPDATE_CURRENT);

This answer was posted as an edit to the question Notification with Multiple actions by the OP Rodrigo Butzke under CC BY-SA 3.0.

vvvvv
  • 25,404
  • 19
  • 49
  • 81