我正在尝试运行SQLite代码,而应用程序在Xamarin Forms App中收到了来自Firebase的通知,首先,我安装此插件Plugin.FirebasePushNotification并添加此权限:
<uses-permission android:name="android.permission.INTERNET" />
然后将此类添加到mu android project
[Application]
public class MainApplication : Application
{
public MainApplication(IntPtr handle, JniHandleOwnership transer) : base(handle, transer)
{
}
public override void OnCreate()
{
base.OnCreate();
//Set the default notification channel for your app when running Android Oreo
if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
{
//Change for your default notification channel id here
FirebasePushNotificationManager.DefaultNotificationChannelId = "FirebasePushNotificationChannel";
//Change for your default notification channel name here
FirebasePushNotificationManager.DefaultNotificationChannelName = "General";
}
//If debug you should reset the token each time.
FirebasePushNotificationManager.Initialize(this, true);
//Handle notification when app is closed here
CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>
{
};
}
}
并且在加载应用程序后的主活动类中,我添加以下行
FirebasePushNotificationManager.ProcessIntent(this, Intent);
并且在我的app.cs中,我像这样处理OnReceived事件
protected override void OnStart()
{
CrossFirebasePushNotification.Current.Subscribe("general");
CrossFirebasePushNotification.Current.OnNotificationReceived += Current_OnNotificationReceived;
}
private void Current_OnNotificationReceived(object source, FirebasePushNotificationDataEventArgs e)
{
var notification = new AJNotification {Id = "1"};
if (e.Data.ContainsKey("body"))
{
notification.Body = $"{e.Data["body"]}";
}
if (e.Data.ContainsKey("title"))
{
notification.Title = e.Data["title"].ToString();
}
if (e.Data.ContainsKey("silent"))
{
notification.Silent = e.Data["silent"].ToString();
}
_sqliteService.SaveItem(notification);
}
然后,我发送了一个静音属性等于true的通知,例如当应用程序已经被“杀死”,然后我从Visual Studio重新运行应用程序并跟踪代码以查看数据是否保存在SQLite中但我没有得到数据
但是您仍然需要在AndroidManifest.xml中添加以上代码,并运行此示例,您将成功获得。