Firebase Xamarin Android FCM本地化

Firebase Xamarin Android FCM本地化,firebase,xamarin.forms,xamarin.android,firebase-cloud-messaging,Firebase,Xamarin.forms,Xamarin.android,Firebase Cloud Messaging,我如何在Xamarin.Android上使用FCM进行本地化?我通过创建带有语言代码的文件夹并将“string.xml”文件放入这些文件夹中,成功地在iOS平台上实现了本地化。 但我试着在android上做,但没有成功。 你们能给我这个示例的链接吗,或者只是解释一下我如何实现它 我还尝试为这个任务创建我的实现。它的工作,但当应用程序被最小化或关闭,这个代码不工作 [Service] [IntentFilter(new[] { "com.google.firebase.MESSAGING_

我如何在Xamarin.Android上使用FCM进行本地化?我通过创建带有语言代码的文件夹并将“string.xml”文件放入这些文件夹中,成功地在iOS平台上实现了本地化。 但我试着在android上做,但没有成功。 你们能给我这个示例的链接吗,或者只是解释一下我如何实现它

我还尝试为这个任务创建我的实现。它的工作,但当应用程序被最小化或关闭,这个代码不工作

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

        }
    }
[服务]
[IntentFilter(新[]{“com.google.firebase.MESSAGING_EVENT”}]
公共类MyFirebaseMessagingService:FirebaseMessagingService
{
const string TAG=“MyFirebaseMsgService”;
公共覆盖无效OnMessageReceived(RemoteMessage)
{
System.Diagnostics.Debug.WriteLine(标记“From:”+message.From);
System.Diagnostics.Debug.WriteLine(标记,“通知消息正文:”+Message.GetNotification()?.Body);
发送通知(消息);
}
公共无效发送通知(RemoteMessage)
{
var intent=新的intent(此,类型为(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent=pendingIntent.GetActivity(this,0,intent,PendingIntentFlags.OneShot);
字符串contentText=null;
if(message.Data.Any())
{
智力资源ID;
尝试
{
resourceId=Resources.GetIdentifier(message.Data.FirstOrDefault(c=>c.Key==“body\u loc\u Key”).Value,
“字符串”,PackageName);
}
catch(Java.Lang.NullPointerException ex)
{
WriteLine($“SendNotification中的异常:{ex.StackTrace}”);
返回;
}
var list=Newtonsoft.Json.JsonConvert.DeserializeObject(message.Data.FirstOrDefault(c=>c.Key==“body\u loc\u args”).Value);
var模式=Resources.GetText(resourceId);
尝试
{
contentText=string.Format(pattern,list.ToArray());
}
捕获(System.FormatException ex)
{
Debug.WriteLine(“异常:+ex.InnerException”);
}
}
var notificationBuilder=new Notification.Builder(此)
.SetSmallIcon(Resource.Drawable.spirallogo)
.SetContentTitle(“自定义标题”)
.SetContentText(contentText??message.GetNotification()?.Body)
.SetAutoCancel(真)
.SetShowWhen(真)
.SetContentIntent(挂起内容);
var notificationManager=notificationManager.FromContext(this);
notificationManager.Notify(0,notificationBuilder.Build());
}
}

我通过更改有效负载来解决问题。让我帮你回答这个问题:

只需将有效负载更改为FCM,然后将get推入处理程序(OnMessageReceived方法)

闲置:

工作:

{
  "registration_ids": ["..."],
  "data": {
    "body_loc_key": "new_chatmessage",
    "body_loc_args": ["mark","Yo!"]
   }
}
{
  "registration_ids": ["..."],
  "data": {
    "body_loc_key": "new_chatmessage",
    "body_loc_args": ["mark","Yo!"]
   }
}