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