FirebasePushNotificationPlugin icon indicating copy to clipboard operation
FirebasePushNotificationPlugin copied to clipboard

custom push notification handler on Android is ignored

Open autosoftmultimedia opened this issue 6 years ago • 3 comments

If you are creating an issue for a BUG please fill out this information. If you are asking a question or requesting a feature you can delete the sections below.

Failure to fill out this information will result in this issue being closed. If you post a full stack trace in a bug it will be closed, please post it to http://gist.github.com and then post the link here.

Bug Information

Version Number of Plugin: 2.3.0 Device Tested On: Nokia 6, Alcatel Idol 4 Simulator Tested On: - Version of VS: 15.6.2 windows Version of Xamarin: 4.9.0.749 Versions of other things you are using:

Steps to reproduce the Behavior

Implement a custom notification handler, inheriting from DefaultPushNotificationHandler and implementing OnBuildNotification method. Send a notification.

Expected Behavior

Custom UI implemented in OnBuildNotification will be displayed

Actual Behavior

custom OnBuildNotification is ignored: nothing happens

Code snippet

#if DEBUG
            FirebasePushNotificationManager.Initialize(this, new SPushNotificationHandler(), false);
#else
            FirebasePushNotificationManager.Initialize(this, new SPushNotificationHandler(), false);
#endif
public class SPushNotificationHandler : DefaultPushNotificationHandler
    {
        public override void OnBuildNotification(NotificationCompat.Builder notificationBuilder, IDictionary<string, object> parameters)
        {
            var tag = string.Empty;
            if (parameters.TryGetValue(TagKey, out object tagContent))
                tag = tagContent.ToString();

            if (!string.IsNullOrEmpty(tag))
                notificationBuilder.SetGroup(tag);

            base.OnBuildNotification(notificationBuilder, parameters);
        }
    }

Screenshots

autosoftmultimedia avatar Mar 15 '18 11:03 autosoftmultimedia

Is this still relevant? Because I also tried to create a custom notification handler and it is not called.

Just asking because I cannot see any code that would be relevant to instantiating the custom notification handler if the app is closed and it would be kinda frustrating to look for the needle in the haystack if there is no needle there.

acuntex avatar Aug 16 '20 14:08 acuntex

This is still an issue in v3.3.10. The overriden OnBuildNotification is not called.

hnabbasi avatar Jun 26 '21 06:06 hnabbasi

Works for me, I implemented to get images from a URL.

internal class CustomPushHandler: DefaultPushNotificationHandler {
  public CustomPushHandler() {}
  public override void OnBuildNotification(NotificationCompat.Builder notificationBuilder, IDictionary < string, object > parameters) {
    if (parameters.TryGetValue("mediaattachment", out var mediaattachment)) {
        notificationBuilder.SetLargeIcon(GetImageBitmapFromUrl(mediaattachment.ToString()));
        notificationBuilder.SetSmallIcon(Resource.Drawable.mnnotification);
    base.OnBuildNotification(notificationBuilder, parameters);
  }
  private Bitmap GetImageBitmapFromUrl(string url) {
    Bitmap imageBitmap = null;

    using(var webClient = new WebClient()) {
      var imageBytes = webClient.DownloadData(url);
      if (imageBytes != null && imageBytes.Length > 0) {
        imageBitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
      }
    }
    return imageBitmap;
  }
}

patkozlowski avatar Jan 21 '23 04:01 patkozlowski