Send notification while in background to API greater than 26, using Notification Channels

Cdn_EuroCdn_Euro Member ✭✭✭
edited August 2018 in Xamarin.Android

Hi guys,
I need some help please.

My app works to send push notifications in the background below API 26. These notifications show up in the notification drawer on Android. They work on an ANY API while in foreground, but above API 26, notifications do not get send in the background.

I understand that I must use notification channels for API 26 and above and I was following a tutorial, but I get an compile error at this point because
noti_chan_urgent is not found.

Also is this a good place to do the background notifications send for API > 26 ?

// this service is used if the app is in the foreground and a message is received
    [Service]
    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
    public class MyFirebaseMessagingService : FirebaseMessagingService
    {
        public const string URGENT_CHANNEL = "com.wj.sa.urgent";

        public override void OnMessageReceived(RemoteMessage message)
        {
            base.OnMessageReceived(message);

            Console.WriteLine("Received: " + message);


            //get current build
            var currentBuild = Build.VERSION.Sdk;

            // check if API > 26 and do the background notification send if true
            if (Int32.Parse(currentBuild) >= 26)
            {
                string chanName = GetString(Resource.String.noti_chan_urgent);     //error happens here
                var importance = NotificationImportance.High;
                NotificationChannel chan =
                   new NotificationChannel(URGENT_CHANNEL, chanName, importance);

                // need to continue here  --- incomplete ---
            }

            try
            {
                var msg = message.GetNotification().Body;

                //pass message to xamarin forms, the picked up by main page and update the label if app is active
                MessagingCenter.Send<object, string>(this, ShakeAlarmRR1.App.NotificationReceivedKey, msg);

            }
            catch (Exception ex)
            {
                Console.WriteLine("Error extracting message: " + ex);
            }
        }
    }

Answers

  • Cdn_EuroCdn_Euro Member ✭✭✭

    UPDATE:

    I do not know if it is the best way but I created the file Reources/strings.xml and added :

    <resources>
      <string name="noti_chan_urgent">ChannelUrgent</string>  
    </resources>
    

    This solved the compilation error, but is this the correct way to solve the problem? And what exactly should be entered in strings.xml ?

    Also is it correct to incorporate the notification channel for APIs >= 26 in the OnMessageReceived() method in the Android project or where should it be located.

    Does anything need to be mofified in the Android Manfest?

  • BillyLiuBillyLiu Member, Xamarin Team Xamurai

    @RRazvan

    This solved the compilation error, but is this the correct way to solve the problem?

    Yes.
    Please refer to the following blog for more information about Android Oreo botification channels:
    https://blog.xamarin.com/android-oreo-notification-channels/

  • Cdn_EuroCdn_Euro Member ✭✭✭

    @BillyLiu I am a junior developer. I am confused where it is best to place that code within a Xamarin cross platform project. Obviously this code with go into the android part of the project.

    Should I make a separate class that inherits from application and initializes the notification channel?

  • BillyLiuBillyLiu Member, Xamarin Team Xamurai

    @RRazvan

    Should I make a separate class that inherits from application and initializes the notification channel?

    Yes, you could try to use DependencyService:
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/

Sign In or Register to comment.