如何在Xamarin.Android上使用FCM进行本地化?我通过创建带有语言代码的文件夹并将"string.xml"文件放入这些文件夹中,成功地在iOS平台上实现了本地化。但我试着在安卓上做,但没有成功。你能提供样本的链接吗?或者只是解释一下我如何实现它?
我也试着创造我对这项任务的认识。它是有效的,但当应用程序最小化或关闭时,此代码不起作用。
[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());
}
}
我通过更改有效负载来解决问题。我帮你回答: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!"]
}
}