CrossGeeks / FirebasePushNotificationPlugin

Firebase Push Notification Plugin for Xamarin iOS and Android
MIT License
396 stars 178 forks source link

custom push notification handler on Android is ignored #122

Open autosoftmultimedia opened 6 years ago

autosoftmultimedia commented 6 years ago

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

acuntex commented 4 years ago

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.

hnabbasi commented 3 years ago

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

patkozlowski commented 1 year ago

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;
  }
}