Android Notification Actions with Notification Channeling
Asked Answered
P

0

1

I am working on an application where we have to generate a notification with action buttons. It was working fine till the date we decided to update our notification handling to support notification channels (released with Android Oreo 8.0). I don't know if this is the reason or there is something missing in our implementation that made notification action buttons unResponsive.

Below mentioned is the code snippet...

NotificationCompat.Builder localCallNotificationBuilder = new NotificationCompat.Builder(mContext, <channelId>);

Intent intent = new Intent();
intent.setAction(IAppConstant.ICallingAction.INTENT_ACTION_DO_CANCEL);
PendingIntent pendingIntentHangUp = PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
localCallNotificationBuilder.addAction(0, mRes.getString(R.string.label_cancel), intent);

localCallNotificationBuilder.setContentText(notificationContent)
            .setTicker(notificationContent)
            .setOnlyAlertOnce(true)
            .setSmallIcon(R.drawable.notification_bar_icon)
            .setLargeIcon(userImageBitmap)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(notificationContent))
            .setOngoing(true);

notificationManager.notify(IAppConstant.IGenericKeyConstants.CALL_NOTIFICATION_ID, localCallNotificationBuilder.build());

I have a receiver class where I am listening to this "INTENT_ACTION_DO_CANCEL". But this receiver is never called.

P.S. I tried the solution mentioned in the link Pending intent is not working on Android O but with no success.

EDIT

Below mentioned is the updated action (with explicit intent as per suggestion mentioned in the link

Intent intentAction = new Intent(mContext, NotificationActionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intentAction, PendingIntent.FLAG_UPDATE_CURRENT);
localCallNotificationBuilder.addAction(0, mRes.getString(R.string.label_cancel), pendingIntent);

<!-- Notification Action Receiver(Manifest entry) -->
<receiver android:name=".reciever.NotificationActionReceiver" />

I am sure there is something missing which I am not able to figure out.

Pollack answered 23/4, 2018 at 11:20 Comment(8)
"I tried the solution" -- not according to the code in the question. If you tried an explicit Intent, show that code.Orvil
@Orvil Updated my code to include explicit intent.Pollack
You have .reciever.NotificationActionReceiver. receiver != reciever. Make sure that your manifest entry matches the actual package name.Orvil
Yeah there isn't any issue with the receiver entry. I have cross checked too and it is working absolutely fine on Android OS < O.Pollack
How are you testing that the receiver is/is not called? For example, if the receiver raises a Notification, perhaps the problem is with that code (e.g., not using a notification channel).Orvil
@Orvil Receiver is there just to respond to notification actions. I had added logs in the receiver to verify if it is being called or not. I tried to launch an activity directly from notification actions using pendingIntent for an activity but without any luck..Pollack
@Orvil : It is working as expected with your suggestion of using an Explicit intent. However, I was doing the same before posting this question but somehow it wasn't working at that time. Thanks for your time and valuable feedback.Pollack
@Pollack can you please update with your working code as answer?Catapult

© 2022 - 2024 — McMap. All rights reserved.