【问题标题】:Xamarin Android FCM localizationXamarin Android FCM 本地化
【发布时间】:2018-12-20 15:20:50
【问题描述】:

如何在 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());

        }
    }

【问题讨论】:

    标签: firebase xamarin.forms xamarin.android firebase-cloud-messaging


    【解决方案1】:

    我通过更改有效负载解决了我的问题。我帮助这个答案:https://stackoverflow.com/a/37876727/7429947

    只需将我的有效负载更改为 FCM,然后将其推送到我的处理程序中(OnMessageReceived 方法)。

    空闲:

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

    工作:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-20
      • 1970-01-01
      • 2014-04-09
      • 1970-01-01
      • 2021-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多