I have encountered a strange behavior trying to make a very simple app. I'm firing a notification and when the user clicks on it I copy some random text to the clipboard. Here's how I fire the notification:
// Build notification
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(this, NotificationBroadcastReceiver.class);
intent.putExtra("test", "hello");
PendingIntent broadcastIntent = PendingIntent.getBroadcast(this, 0, intent, Intent.FILL_IN_DATA | PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setContentTitle("Hello!")
.setContentText("Hi!")
.setSmallIcon(R.drawable.icon)
.setContentIntent(broadcastIntent)
.setOngoing(false)
.setDefaults(Notification.DEFAULT_ALL)
.setTicker("Click me!")
.setAutoCancel(true);
notificationManager.notify(0, notification.build());
And this is my BroadcastReceiver
:
public class NotificationBroadcastReceiver extends BroadcastReceiver {
public NotificationBroadcastReceiver() {
}
@Override
public void onReceive(Context context, Intent
// Copy the text to the clipboard when notification is clicked
ClipboardManager clipboard = (ClipboardManager) context.getSystemService(context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("", "");
clipboard.setPrimaryClip(clip);
}
}
I'm just copying an empty string to the clipboard. On this way, when I click on the notification it doesn't goes off as it should when setting setAutoCancel(true)
, BUT if I remove the line:
clipboard.setPrimaryClip(clip);
From the BroadcastReceiver
then the notification is dismissed on click as it should. Why is this happening?