0

I am having an issue on Oreo devices where notifications are simply just not displayed in the hud when they are sent to client. I can assure that they are being received in the background as i've created a custom vibration effect OnMessageReceived and this works but still nothing is displayed in the Android HUD.

Also, I've noticed another article saying Visual Studio kills the notification service when forcefully closed. This is true because it also killed my vibration effect, but with the app side loaded I can launch from the device and my custom effect is triggered.

I believe what I am doing is 1, registering my tags to the Azure-FCM service. 2nd, I am saying registering my API Endpoint and also the allowed templates. I think there is something wrong with the payload templates I am registering, and the payloads I am sending

See below..

Web API that generates Android payload
private string GetAndroidAlertJson(int? fromId, int notificationType, string message)
    {
        return new JObject(


           new JProperty("data",
               new JObject(
                   new JProperty("notification",

                       new JObject(
                           new JProperty("badge", 1),
                           new JProperty("body", message),
                           new JProperty("title", "wazzup?!"),
                           new JProperty("priority", "high")
                        )
                    ),
                   new JProperty("type_id", notificationType),
                   new JProperty("user", fromId != null ? fromId.ToString() : ""),
                   new JProperty("message", message)
                )
           )
       ).ToString();
    }

Xamarin.Android

//NotificationBuilder
return new NotificationCompat.Builder(ApplicationContext, PRIMARY_CHANNEL)
                 .SetContentTitle(title)
                 .SetContentText(body)
                 .SetSmallIcon(SmallIcon)
                 .SetPriority(2)
                 .SetVibrate(new long[0])
                 .SetAutoCancel(true);
//In MainActivity
public NotificationHelper(Context context) : base(context)
    {
        var chan1 = new NotificationChannel(PRIMARY_CHANNEL,
                PRIMARY_CHANNEL, NotificationImportance.Max);
        chan1.LightColor = 2;
        chan1.LockscreenVisibility = NotificationVisibility.Public;
        Manager.CreateNotificationChannel(chan1);
    }
public NotificationCompat.Builder GetNotification1(String title, String body)
    {
        return new NotificationCompat.Builder(ApplicationContext, PRIMARY_CHANNEL)
                 .SetContentTitle(title)
                 .SetContentText(body)
                 .SetSmallIcon(SmallIcon)
                 .SetPriority(2)
                 .SetVibrate(new long[0])
                 .SetAutoCancel(true);
    }
 FCMService
 [Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class FCMMessagingService : FirebaseMessagingService
{//Foreground Only
    const string TAG = "MyFirebaseMsgService";
    public override void OnMessageReceived(RemoteMessage message)
    {
        if (Debugger.IsAttached)
            Debugger.Break();
        CustomNotificationEffect();
        try
        {
            if (message.Data.Count > 0)
            {
                var type_id = Convert.ToInt32(RetreiveNotification("type_id"));
                var body = RetreiveNotification("message");
                MainActivity.SendNotification(type_id, body);
            }
        }
        catch (Exception e)
        {
            //Was NotificationType null? this should never be null anyways
        }
        string RetreiveNotification(string key)
        {
            string value = String.Empty;
            message.Data.TryGetValue(key, out value);
            return value;
        }
    }
[Service]
[IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
public class FCMInstanceIDService : FirebaseInstanceIdService
{
    const string TAG = "MyFirebaseIIDService";
    public static string ProfilePushTag;
    public async override void OnTokenRefresh()
    {
        var refreshedToken = FirebaseInstanceId.Instance.Token;
        Console.WriteLine("Token Received!: " + refreshedToken);
        await SendRegistrationToServer(refreshedToken);
    }
    public async Task SendRegistrationToServer(string token)
    {
        //We will have to send our token to the server here
        //

        string templateBodyFCM =
            "{" +
            "\"notification\" : {" +
            "\"body\" : \"$(messageParam)\"," +
            "\"title\" : \"Xamarin U\"," +
            "\"icon\" : \"myicon\" }}";
        var templates = new JObject();
        //templateBodyFCM = "Pyx";
        templates["genericMessage"] = new JObject
        {
            { "body", templateBodyFCM}
        };


        try
        {

            var tags = new List<string>();
            if (!String.IsNullOrWhiteSpace(ProfilePushTag))
            {
                // Register with Notification Hubs
                var hub = new NotificationHub("HUB-NAME",
                                      "AZURE-ENDPOINT"
                                      , this);
                tags.Add(ProfilePushTag);
                var regID = hub.Register(token, tags.ToArray()).RegistrationId;



                var hub2 = new MobileServiceClient("https://MyAPI");
                await hub2.GetPush().RegisterAsync(token, templates);
            }
        }
        catch (Exception e)
        {

        }
    }
sme
  • 4,023
  • 3
  • 28
  • 42
XamDev89
  • 193
  • 3
  • 13
  • In FCM The client registers the acceptable payload templates received. Maybe a clue is the template I am using to register w/ the server which is string templateBodyFCM = "{" + "\"notification\" : {" + "\"body\" : \"$(messageParam)\"," + "\"title\" : \"Xamarin U\"," + "\"icon\" : \"myicon\" }}"; – XamDev89 Aug 20 '18 at 05:16
  • I've just read that the key "Notification" does not work in background https://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase – XamDev89 Aug 20 '18 at 05:17
  • Did you create a notification channel for oreo? if not check this out https://medium.com/globallogic-latinoamerica-mobile/firebase-cloud-messaging-warning-updating-to-android-oreo-1343fe894bd5 – FreakyAli Aug 20 '18 at 06:31
  • Yes I did create a NotificationChannel & also use NotificationManager.CreateFromNotificationChannel method... I Think the answer MAY lye here https://stackoverflow.com/questions/50643502/fcm-cannot-show-pop-up-when-app-in-the-background – XamDev89 Aug 20 '18 at 07:26
  • Yeah so why don't you change your payload then? – FreakyAli Aug 20 '18 at 07:27
  • Sorry I was wrong, I've changed both the payload template on client, and sent directly from Azure console, both w/ no notification key and this is not the answer. I can say this... Test sends coming directly from Firebase Console work in background/foreground and are displayed in the HUD. Test sends coming directly from my API or Azure console also work in Foreground/Background, but nothing is displayed in Android HUD. – XamDev89 Aug 20 '18 at 08:35
  • Then there must be some configuration issue from Azure side you should check for that may be – FreakyAli Aug 20 '18 at 08:37

2 Answers2

1

In Android O it's a must to use a channel with your Notification Builder below is a sample code of how you should use it :

public NotificationCompat.Builder GetNotification1(String title, String body) {
 var mynotif = new Notification.Builder(ApplicationContext)
  .SetContentTitle(title)
  .SetContentText(body)
  .SetSmallIcon(SmallIcon)
  .SetPriority(2)
  .SetVibrate(new long[0])
  .SetAutoCancel(true);
 if (Build.VERSION.SdkInt >= BuildVersionCodes.O) {
  mynotif.SetChannelId(PRIMARY_CHANNEL);
 }

}
Houssem
  • 1,042
  • 7
  • 16
0

If you look at my Web API, everything is wrapped inside a "data" type. According to this article How to handle notification when app in background in Firebase the "data" types will not be displayed in the HUD, but the "notification" types will. removing this attribute resolved the issue and is now displayed in the HUD.

XamDev89
  • 193
  • 3
  • 13