3

I followed the tutorial to add push notifications to a Xamarin Forms app. It works in iOS in all states: app not running or running in the background or active. For android I get the notifications when the app is not running or it's in the background but not when the app is active.

I am using:

Xamarin.Azure.NotificationHubs.Android 1.1.4.1. (1/28/2021)

Xamarin.Firebase.Messaging 122.0.0 (6/11/2021)

Heres in my code on Android:

public class MainActivity : FormsAppCompatActivity
{
    internal static readonly string CHANNEL_ID = "xxxxxx";
    const string NotificationHubConnectionString = "xxxxxx";
    const string NotificationHubName = "xxxxxx";

    protected override void OnCreate(Bundle bundle)
    {
        . 
        .  
        .  

        NotificationHub.SetListener(new AzureListener());
        NotificationHub.Start(this.Application, NotificationHubName, NotificationHubConnectionString);

        . 
        .  
        .  

        LoadApplication(new App());
    }

}
    
    
public class AzureListener : Java.Lang.Object, INotificationListener
{
    public void OnPushNotificationReceived(Context context, INotificationMessage message)
    {
        var intent = new Intent(context, typeof(MainActivity));
        intent.AddFlags(ActivityFlags.ClearTop);
        var pendingIntent = PendingIntent.GetActivity(context, 0, intent, PendingIntentFlags.OneShot);

        var notificationBuilder = new NotificationCompat.Builder(context, MainActivity.CHANNEL_ID);

        notificationBuilder.SetContentTitle(message.Title)
                    .SetSmallIcon(Resource.Drawable.icon_category_announcement)
                    .SetContentText(message.Body)
                    .SetAutoCancel(true)
                    .SetShowWhen(false)
                    .SetContentIntent(pendingIntent);

        var notificationManager = NotificationManager.FromContext(context);

        notificationManager.Notify(0, notificationBuilder.Build());
    }
}

I followed the tutorial from Microsoft (1/12/2021):

https://learn.microsoft.com/en-us/azure/notification-hubs/xamarin-notification-hubs-push-notifications-android-gcm#set-up-notification-hubs-in-your-project

In the tutorial there seem to be an issue on step 11 and 12 because they talk about a class name from the previous tutorial but then they use the new class name and some instructions seem to be missing:

  1. Add the following above your class declaration, and have your class inherit from Java.Lang.Object and implement the INotificationListener:

=> There is nothing above the class declaration

  1. Add the following code inside MyFirebaseMessagingService class, to process messages that are received.

=> The class name is actually AzureListener

What am I missing? How can I make the push notification to show up when the app is running and active?

UPDATE: I think I was kind of confused here thinking that this is the code that shows the notification when the app in not running or running in the background. It looks like this code has nothing to do with that. The notification is shown by the OS. This code is to handle/show the notification when the app is active in the foreground. So in that case I am ok with showing just an alert. I was able to make this work simply doing this:

public class AzureListener : Java.Lang.Object, INotificationListener
{
    public void OnPushNotificationReceived(Context context, INotificationMessage message)
    {
        Device.BeginInvokeOnMainThread(() =>
        {
            Application.Current.MainPage.DisplayAlert(message.Title, message.Body, "OK");
        });
    }
}

I am still not sure why the first code does not show the notification when the app is running in the foreground but for now I am ok with the alert.

Juan
  • 31
  • 3
  • Given that the problem is android-specific, perhaps [the answers to this Android question](https://stackoverflow.com/q/56521530/199364) will help. That is in java, and is not related to Azure, but those answers do explain Android's requirements re notifications while app is active. – ToolmakerSteve Jun 30 '21 at 12:34
  • Thanks Steve. I had seem some code similar to that but I think the idea is to take advantage of the WindowsAzure.Messaging.NotificationHubs. I looked at the link and it says: "add custom method to show notification using NotificationCompat.Builder". If you see the code I provided, taken from the tutorial, It is using NotificationCompat.Builder. – Juan Jun 30 '21 at 17:56
  • If you want to use Azure notification, you need to check the seetings in the link below. https://learn.microsoft.com/en-us/azure/developer/mobile-apps/notification-hubs-backend-service-xamarin-forms#configure-the-native-ios-project-for-push-notifications – Wendy Zang - MSFT Jul 01 '21 at 07:40
  • Thanks Wendy, it looks like I found a work around for now. I added an update to the question above. I had looked and that link before but that's more to implement server side, we are actually using REST API as backend. The issue here is more on the mobile app side. – Juan Jul 01 '21 at 07:56

1 Answers1

0

It might be because of the structure of the payload you are sending from Azure. Please try a payload similar to this it will work. I was also facing similar issues initially.

    {
  "to": "-------------Token--------------------",
  "data": {
     "title": "title",  
     "message": "message"
   }
}

Please read the payload part for more details.

https://github.com/CrossGeeks/PushNotificationPlugin/blob/master/docs/AndroidCustomization.md

Similar Question

Resume App from a Firebase Notification is not working (Xamarin Forms)

Rakesh R Nair
  • 1,736
  • 2
  • 12
  • 30
  • 1
    Thanks for your reply Rakesh. The payload that I am sending is: { "notification":{ "title":"Title", "body":"This is a sample notification delivered by Azure Notification Hubs." } } And that works fine when the app is inactive or not running. But not when the app is active (displaying on the screen) I tried the payload the way you suggested but the notifications did not show at all. – Juan Jun 30 '21 at 16:59