【发布时间】:2020-07-19 20:52:06
【问题描述】:
我正在使用 Firebase 云消息传递。
当我收到推送通知时,我有两种情况:
- 当应用程序在前台(或正在运行)时,我使用以下代码保存数据
public override void OnMessageReceived(RemoteMessage message)
{
var body = message.GetNotification().Body;
var title = message.GetNotification().Title;
string[] notificationMessage = { title, body };
MessagingCenter.Send<object, string[]>(this, Constants.TOPIC, notificationMessage);
Preferences.Set("title", message[0]);
Preferences.Set("body", message[1]);
SendNotification(title, body, message.Data);
}
上面的代码运行良好,数据保存在首选项中。
- 但是当它没有运行时,当我收到通知时它没有得到保存。 有什么方法可以在 Xamarin Forms 中的 Android 和 iOS 中做到这一点
更新
#FirebaseMessageService
public class UWTFirebaseMessagingService : FirebaseMessagingService
{
public override void OnMessageReceived(RemoteMessage message)
{
var body = message.GetNotification().Body;
var title = message.GetNotification().Title;
string[] notificationMessage = { title, body };
MessagingCenter.Send<object, string[]>(this, Constants.TOPIC, notificationMessage);
SendNotification(title, body, message.Data);
}
void SendNotification(string messageTitle, string messageBody, IDictionary<string, string> data)
{
var intent = new Intent(this, typeof(MainActivity));
intent.PutExtra("title", messageTitle);
intent.PutExtra("body", messageBody);
intent.AddFlags(ActivityFlags.ClearTop);
foreach (var key in data.Keys)
{
intent.PutExtra(key, data[key]);
}
var pendingIntent = PendingIntent.GetActivity(this,
MainActivity.NOTIFICATION_ID,
intent,
PendingIntentFlags.OneShot);
var notificationBuilder = new NotificationCompat.Builder(this, Constants.CHANNEL_ID)
.SetSmallIcon(Resource.Drawable.icon)
.SetContentTitle(messageTitle)
.SetContentText(messageBody)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
var notificationManager = NotificationManagerCompat.From(this);
notificationManager.Notify(Constants.NOTIFICATION_ID, notificationBuilder.Build());
}
}
主要活动
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
RequestPermissions(Permission, RequestId);
HtmlLabelRenderer.Initialize();
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
Xamarin.FormsMaps.Init(this, savedInstanceState);
GAService.GetGASInstance().InitializeNativeGAS(this);
CreateNotificationChannel();
FirebaseMessaging.Instance.SubscribeToTopic(Constants.TOPIC);
LoadApplication(new App());
if (Intent.Extras != null)
{
foreach (var key in Intent.Extras.KeySet())
{
if (key == "title")
{
var value = Intent.Extras.GetString(key);
Preferences.Set("title", value);
}
else if (key == "body")
{
var value = Intent.Extras.GetString(key);
Preferences.Set("body", value);
}
}
}
}
void CreateNotificationChannel()
{
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
// Notification channels are new in API 26 (and not a part of the
// support library). There is no need to create a notification
// channel on older versions of Android.
return;
}
var channel = new NotificationChannel(Constants.CHANNEL_ID,
Constants.CHANNEL_NAME,
NotificationImportance.Default)
{
Description = Constants.CHANNEL_DESCRIPTION
};
var notificationManager = (NotificationManager)GetSystemService(Android.Content.Context.NotificationService);
notificationManager.CreateNotificationChannel(channel);
}
//for foreground and backgrounded state
protected override void OnNewIntent(Intent intent)
{
if (intent != null)
{
var title = intent.GetStringExtra("title");
var body = intent.GetStringExtra("body");
Preferences.Set("title", title);
Preferences.Set("body", body);
}
}
}
【问题讨论】:
-
嗨,你能分享你的通知负载结构吗?
-
@Anand 我用过这个教程。docs.microsoft.com/en-us/xamarin/android/data-cloud/…
标签: android ios firebase xamarin.forms firebase-cloud-messaging