FirebasePushNotificationPlugin icon indicating copy to clipboard operation
FirebasePushNotificationPlugin copied to clipboard

NotificationUserActions are not displaying

Open LeoJHarris opened this issue 3 years ago • 5 comments

Having a little issue here trying to display NotificationUserActions on android in that its simply not displaying despite OnNotificationReceived being triggered on target devices.

The setup in the MainActivity follows sample code and trying to call the "request" NotificationUserCategory:

            if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
            {
                //Change for your default notification channel id here
                FirebasePushNotificationManager.DefaultNotificationChannelId = "DefaultChannel";

                //Change for your default notification channel name here
                FirebasePushNotificationManager.DefaultNotificationChannelName = "General";
            }

            FirebasePushNotificationManager.Initialize(this, new NotificationUserCategory[]
            {
                new NotificationUserCategory("message",new List<NotificationUserAction>
                {
                    new NotificationUserAction("Reply","Reply",NotificationActionType.Foreground),
                    new NotificationUserAction("Forward","Forward",NotificationActionType.Foreground)
                }),
                new NotificationUserCategory("request",new List<NotificationUserAction>
                {
                    new NotificationUserAction("Accept", "Accept",NotificationActionType.Default,string.Empty),
                    new NotificationUserAction("Decline", "Decline",NotificationActionType.Default,string.Empty)
                })
            },
#if DEBUG
            true
#else
            false
#endif
            );

            CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>
            {
                System.Diagnostics.Debug.WriteLine("NOTIFICATION RECEIVED", p.Data);
            };

My model class looks like this (android):

public class FCMBody
    {
        public FCMNotification notification { get; set; }

        public string[] registration_ids { get; set; }

        public FCMData data { get; set; }

        public string condition { get; set; } = string.Empty;

        public string priority { get; set; }
    }

    public class FCMNotification
    {
        public string body { get; set; } = string.Empty;

        public string title { get; set; } = string.Empty;
    }

    public class FCMData
    {
        public string click_action { get; set; } = string.Empty;

        public string key1 { get; set; } = string.Empty;

        public string key2 { get; set; } = string.Empty;

        public string key3 { get; set; } = string.Empty;

        public string key4 { get; set; } = string.Empty;
    }

Finally sending the FCMBody instance is called through this method:

public static async Task<bool> SendNotification(FCMBody notification)
        {
            try
            {
                var httpContent = JsonConvert.SerializeObject(notification);
                var client = new HttpClient();
                var authorization = string.Format("key={0}", Constants.FirebaseServerKey);
                client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", authorization);
                var stringContent = new StringContent(httpContent);
                stringContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                const string uri = "https://fcm.googleapis.com/fcm/send";
                var response = await client.PostAsync(uri, stringContent).ConfigureAwait(false);
                var result = response.Content.ReadAsStringAsync();
                if (response.IsSuccessStatusCode)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (TaskCanceledException ex)
            {
                return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

There are no problems in sending the message and the OnNotificationReceived gets called with data to targetted devices but how to get the NotificationUserCategory to work which is what I need since I want the user to have two actions on the notification i.e. accept or decline actions.

I construct and send the FCM notification like the below and set the click_action as "request":

 FCMBody body = new FCMBody()
                    {
                        notification = new FCMNotification
                        {
                            body = "yol",
                            title = "Hey",
                        },
                        data = new FCMData
                        {
                            click_action = "request"
                        },
                        registration_ids = invitedPersons.Select(person => person.FcmToken).ToArray(),
                    };

                    bool isSuccessCall = await Utils.SendNotification(body).ConfigureAwait(true);

And as shown further up in MainActivity I have a NotificationUserCategory with the category as "request" which provides actions "Accept" and "Decline". Am I missing something here? I know the notification gets sent and recieved on targetted devices but NotificationUserCategory is not being shown for "request".

OnNotificationReceivedCalled with the instance model sent:

Capture

LeoJHarris avatar Sep 26 '20 06:09 LeoJHarris

Nobody has NotificationUserActions working..?

LeoJHarris avatar Sep 29 '20 03:09 LeoJHarris

I have exactly the same code, same problem, the shown message has title and body but no buttons, did you solved it out?

XamDevToo avatar Oct 02 '20 00:10 XamDevToo

@XamDevToo No. I could not solve it, I have moved on but I will need to come back and try again. Oddly it worked only once when I was testing and fiddling with sending notifications, when I tried again with notification it didnt work again. So I got the notification showing once with NotificationUserActions for request, but no I cannot get it showing again no matter how many times I try to send notification with the action set request. I have followed exactly as the samples show but it is not consistiently showing the notifications with actions.. @rdelrosario any ideas?

LeoJHarris avatar Oct 02 '20 01:10 LeoJHarris

@XamDevToo from my understanding if you have a UserNotificationCategory key such as request and the notification contains the value request set for click_action then the NotificationUserAction should work and resolve to that UserNotificationCategory? Maybe I am mistaken?

LeoJHarris avatar Oct 02 '20 01:10 LeoJHarris

Any updates on this? I have tried for a while setting click_action to request or message and still no buttons are appearing -- I have both request and message UserNotificationCategories set up following the code examples/sample for this nuget. Thanks in advance.

rsbepvb avatar Jun 04 '21 19:06 rsbepvb