How I can do localization with FCM on Xamarin.Android? I successfully implemented localization on iOS platform by creating folders with language codes and put in these folders "string.xml" files. But I tried do it on android but unsuccessfully. Can you give link to sample or just explain how i can implement it?
Also I tried create my realization for this task. It's works but when app is minimized or closed this code doesn't work.
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
const string TAG = "MyFirebaseMsgService";
public override void OnMessageReceived(RemoteMessage message)
{
System.Diagnostics.Debug.WriteLine(TAG, "From: " + message.From);
System.Diagnostics.Debug.WriteLine(TAG, "Notification Message Body: " + message.GetNotification()?.Body);
SendNotification(message);
}
public void SendNotification(RemoteMessage message)
{
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
string contentText = null;
if (message.Data.Any())
{
int resourceId;
try
{
resourceId = Resources.GetIdentifier(message.Data.FirstOrDefault(c => c.Key == "body_loc_key").Value,
"string", PackageName);
}
catch (Java.Lang.NullPointerException ex)
{
Debug.WriteLine($"Exception in SendNotification: {ex.StackTrace}");
return;
}
var list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(message.Data.FirstOrDefault(c => c.Key == "body_loc_args").Value);
var pattern = Resources.GetText(resourceId);
try
{
contentText = string.Format(pattern, list.ToArray());
}
catch (System.FormatException ex)
{
Debug.WriteLine("Exception: " + ex.InnerException);
}
}
var notificationBuilder = new Notification.Builder(this)
.SetSmallIcon(Resource.Drawable.spirallogo)
.SetContentTitle("Custom Title")
.SetContentText(contentText ?? message.GetNotification()?.Body)
.SetAutoCancel(true)
.SetShowWhen(true)
.SetContentIntent(pendingIntent);
var notificationManager = NotificationManager.FromContext(this);
notificationManager.Notify(0, notificationBuilder.Build());
}
}