Pending notifications in notifications

I would like to show a notification that shows the progress of the current work. This works well for me. But at the same time, the remote view must contain a cancel button to stop the current operation. The usual intention of the content should still do something else, i.e. Do not cancel the current operation. It seems that I can only have one intention.

I need to specify the contentIntent, which starts when I click on the notification: if I do not indicate that I get something along these lines:

E/ActivityManager( 62): Activity Manager Crash E/ActivityManager( 62): java.lang.IllegalArgumentException: contentIntent required ... 

For the Cancel button, I set a different intent:

 Intent cancelSyncIntent = new Intent("com.xyz.CANCEL_SYNC"); contentView.setOnClickPendingIntent(R.id.cancel_sync, PendingIntent.getBroadcast(context, 0, cancelSyncIntent, 0)); 

But it never works. I always get content when the button is clicked. Looks like I can’t use buttons in remote notifications ?!

Perhaps I could display the text: "<<Press to cancel the operation β†’", but it seems rather heavy.

Update: Afer J recommendations:

  final Notification n = new Notification(R.drawable.gen_auto_notification_icon, context.getResources() .getString( fastSyncOnly ? R.string.fast_synchronization_running_notification_title : R.string.synchronization_running_notification_title), new Date().getTime()); n.flags = Notification.FLAG_ONGOING_EVENT; final RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.in_progress_notification); n.contentView = contentView; // Intent cancelSyncIntent = new Intent("com.newsrob.CANCEL_SYNC"); Intent cancelSyncIntent = new Intent(); cancelSyncIntent.setClass(context, FireReceiver.class); PendingIntent pendingCancelSyncIntent = PendingIntent.getActivity(context, 0, cancelSyncIntent, 0); contentView.setOnClickPendingIntent(R.id.cancel_sync, pendingCancelSyncIntent); Intent showDashboardIntent = new Intent(context, DashboardListActivity.class); PendingIntent showDashboardPendingIntent = PendingIntent.getActivity(context, 0, showDashboardIntent, 0); n.contentIntent = showDashboardPendingIntent; 
+2
source share
2 answers

Short answer: in the notification view you cannot view interactive view images.

Longer answer ...

I spent the whole day trying to get this to work, and then gave up and looked at the source of Android. Everything seems to be happening in makeNotificationView () in StatusBarService.java. There, the code inflates a layout with the name "status_bar_latest_event" for each line in the tender for the drop-down notification. It grabs the ViewGroup from this view, it simply inflates the name "content", calls setDescendantFocusability (ViewGroup.FOCUS_BLOCK_DESCENDANTS) on it, and then adds your notification as a child. Therefore, assuming that I understand how FOCUS_BLOCK_DESCENDANTS works, it seems that there is no hope that your eyes or any of his children will receive focus or clicks.

I also followed the logic of how the main PendingEvent works, which you associate with your notification. The same ViewGroup that hosts your Notification view receives a call to setOnClickListener () to register a listener class called "Launcher". When this class receives the onClick () event, it simply calls the send () method on the PendingIntent that you associated with the notification. I was hoping that the click coordinate could be transmitted together, so I could at least try to figure out which button was clicked, but there is no such luck.

In any case, if anyone knows a way around this, I would like to hear it. For now, I just pull out my buttons and move on.

+3
source

Intent cancelSyncIntent = new Intent (); cancelSyncIntent.setClassName ("com.xyz", "com.xyz.CANCEL_SYNC");

 PendingIntent pendingIntent = PendingIntent.getActivity(context, 0 /* no requestCode */, cancelSyncIntent , 0 /* no flags */); contentView.setOnClickPendingIntent(R.id.cancel_sync, pendingIntent); 
0
source

Source: https://habr.com/ru/post/899929/


All Articles