Quiet  Sad
Quiet Sad

Reputation: 19

Xamarin Android background services

I've followed several tutorials about custom services running while the app is on background or killed (included Xamarin's), but I fail to make it work myself.

What I pretend:

1- A Firebase service sends notifications to the device. This service does work on background/killed

2- My custom service must perform a small function whenever the fcm service does. Specially if the app is on background/killed. My service does operate if the app is running on foreground.

Could anybody point out where am I failing? (Or help me to understand better background services?)

My code so far:

CUSTOM SERVICE

using Android.App;
using Android.Content;
using Android.OS;
using Android.Util;
using ME.Leolin.Shortcutbadger;

namespace App1
{
    [Service]
    public class Badge_Service : Service
    {
        int n = 0;
        public IBinder Binder { get; private set; }

        public override IBinder OnBind(Intent intent)
        {
            Log.Debug("SS", "OnBind");
            this.Binder = new Badge_Binder(this);
            add_up(n); //custom function
            return this.Binder;
        }

        public override bool StopService(Intent name)
        {
            return base.StopService(name);
        }

        public void add_up(int count)
        {
            count++;
            ShortcutBadger.ApplyCount(ApplicationContext, count);
        }
    }

    public class Badge_Binder : Binder
    {
        public Badge_Service Service { get; private set; }

        public Badge_Binder(Badge_Service Service)
        {
            this.Service = Service;
        }
    }

    public class Badge_Conection : Java.Lang.Object, IServiceConnection
    {
        MyFirebaseMessagingService activity;

        public bool IsConnected { get; private set; }
        public Badge_Binder Binder { get; private set; }

        public Badge_Conection(MyFirebaseMessagingService activity)
        {
            IsConnected = false;
            Binder = null;
            this.activity = activity;
        }

        public void OnServiceDisconnected(ComponentName name)
        {
            Log.Debug("SS", "Disconnected");
            IsConnected = false;
            Binder = null;
        }

        public void OnServiceConnected(ComponentName name, IBinder Service)
        {
            Binder = Service as Badge_Binder;
            IsConnected = this.Binder != null;
            Log.Info("SS", "Connected");
        }
    }
}

FIREBASE IMPLEMENTATION

using Android.App;
using Android.Content;
using Firebase.Iid;
using Android.Support.V7.App;
using Android.Media;
using Firebase.Messaging;

namespace App1
{
    [Service]
    [IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
    public class MyFirebaseServices : FirebaseInstanceIdService
    {
        public override void OnTokenRefresh()
        {
            base.OnTokenRefresh();
            MClass.IDfcm = FirebaseInstanceId.Instance.Token;   //MClass just stores variables for later use.
            Android.Util.Log.Debug("Refreshed Token:", MClass.IDfcm);
        }
    }

    [Service]
    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
    public class MyFirebaseMessagingService : FirebaseMessagingService
    {
        public override void OnMessageReceived(RemoteMessage msg)
        {
            base.OnMessageReceived(msg);
            sendNotification(msg.GetNotification().Body);

            Intent serviceToStart = new Intent(this, typeof(Badge_Service));
            BindService(serviceToStart, new Badge_Conection(this), Bind.AutoCreate);
        }

        private void sendNotification(string msg)
        {
            var activity = new Intent(this, typeof(LogIn)); //LogIn is my MainActivity
            activity.AddFlags(ActivityFlags.ClearTop);
            var  screen = PendingIntent.GetActivity(this, 0, activity, PendingIntentFlags.OneShot);
            var sound = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
            var notif = new NotificationCompat.Builder(this)
                .SetSmallIcon(Resource.Drawable.Icon)
                .SetContentTitle("New Notification")
                .SetContentText(msg)
                .SetAutoCancel(true)
                .SetSound(sound)
                .SetContentIntent(screen);

            var controller = NotificationManager.FromContext(this);
            controller.Notify(0, notif.Build());
        }
    }
}

Upvotes: 0

Views: 2282

Answers (1)

Quiet  Sad
Quiet Sad

Reputation: 19

I resolved my issue some time ago, as I see nobody else answered it, I will post my solution in case someone else has this problem:

The failure was as simple as that OnMessageReceived is never called on background. I bypassed this by using HandleIntent instead, which made my background service unecesary, as this: (the class is the same):

    public override void HandleIntent(Intent intent)
    {
        string msg = intent.GetStringExtra("body");
        if (msg == null) msg = "New Notification";


        ShortcutBadger.ApplyCount(this, Global.countNotifications());
        sendNotification(msg);
    }

Extra Note

HandleIntent can't use RemoteMessage type, so (if I am not mistaken) it is unable to retrieve notifications' bodies.

A custom notification sender is needed in order to be able to retrieve manually the body as a json.

This is how I built mine:

(Warning: The code may not be so neat. I reached that stage where I kind of hammered the keyboard with my head D:):

    public static string signalFCM(string msg, string user_firebase_id) //This must be gathered by storing a firebase_id created by an android device.
    {
        try
        {
            WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
            tRequest.Method = "post";
            tRequest.ContentType = "application/json";
            var data = new
            {
                to = user_firebase_id,
                notification = new  // *** THIS IS HOW FIREBASE SENDS THEM BY DEFAULT
                {
                    body = msg,
                    title = "MyAPP",
                    sound = "Enabled"
                },
                // **** THIS IS THE CUSTOM EXTRA TO RETRIEVE ITS CONTENT ****
                data = new
                {
                    body = msg,
                    title = "MyAPP",
                    payload = "1"
                }
                // ******************
            };

            var serializer = new JavaScriptSerializer();
            var json = serializer.Serialize(data);
            Byte[] byteArray = Encoding.UTF8.GetBytes(json);
            tRequest.Headers.Add(string.Format("Authorization: key={0}", APP_KEY)); //This must be gathered from the Firebase Console APP page
            tRequest.Headers.Add(string.Format("Sender: id={0}", APP_ID)); //This must be gathered from the Firebase Console APP page
            tRequest.ContentLength = byteArray.Length;

            using (Stream dataStream = tRequest.GetRequestStream())
            {
                dataStream.Write(byteArray, 0, byteArray.Length);
                using (WebResponse tResponse = tRequest.GetResponse())
                {
                    using (Stream dataStreamResponse = tResponse.GetResponseStream())
                    {
                        using (StreamReader tReader = new StreamReader(dataStreamResponse))
                        {
                            String sResponseFromServer = tReader.ReadToEnd();
                            return sResponseFromServer;
                        }
                    }
                }
            }
        }
        catch (Exception ex)    {   return ex.Message;    }
    }
}

Upvotes: 1

Related Questions