PushNotificationPlugin icon indicating copy to clipboard operation
PushNotificationPlugin copied to clipboard

Custom Push notification

Open Monikaprime opened this issue 5 years ago • 1 comments

how can I have custom notification builder in xamarin when using this plugin

Monikaprime avatar Jul 20 '20 06:07 Monikaprime

Create a custom PushNotificationHandler like this:

    public class PushNotificationHandler: DefaultPushNotificationHandler
    {
        public override void OnBuildNotification(NotificationCompat.Builder notificationBuilder, IDictionary<string, object> parameters)
        {
            base.OnBuildNotification(notificationBuilder, parameters);
            try
            {
                if (!parameters.TryGetValue("image", out object imageUrlObject))
                    return;
                string imageUrl = imageUrlObject.ToString();
                if (string.IsNullOrEmpty(imageUrl))
                    return;
                using (HttpClient httpClient = new HttpClient())
                {
                    byte[] imageBytes = httpClient.GetByteArrayAsync(imageUrl).Result;
                    Bitmap image = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
                    notificationBuilder.SetLargeIcon(Bitmap.CreateBitmap(image));
                }
            }
            catch (Exception)
            {
            }
        }
    }

Then where you initialize do:

            FirebasePushNotificationManager.Initialize(this, new PushNotificationHandler(), false, true, true);

Just make sure the notification has no "notification" object while sending or the handler won't be called. It only works with data-payload.

acuntex avatar Sep 04 '20 14:09 acuntex