收到通知Xamarin表单时运行Sqlite代码 [英] Run Sqlite Code when received notification Xamarin Forms

查看:86
本文介绍了收到通知Xamarin表单时运行Sqlite代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在Xamarin Forms App中收到来自Firebase的通知时,我正在尝试运行SQLite代码,首先,我安装此插件Plugin.FirebasePushNotification并添加此权限:

I am trying to run SQLite code while app received notification from firebase in Xamarin Forms App, First I Install this plugin Plugin.FirebasePushNotification and also add this permission:

<uses-permission android:name="android.permission.INTERNET" />

然后将此类添加到mu android项目

then add this class to 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) =>
        {

        };
    }
}

在加载应用程序后的主活动类中,我添加了这一行

and in my main activity class after load app, I add this line

FirebasePushNotificationManager.ProcessIntent(this, Intent);

在我的app.cs中,我像这样处理OnReceived事件

and in my app.cs I handle OnReceived Event like this

        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中但我没有得到数据

then I send a notification with silent property equal true like when the app is already "Killed" then I rerun app from Visual Studio and track code to see if data saved in SQLite but I didn't get data

推荐答案

请编辑AndroidManifest.xml并将以下元素插入该部分:

Please edit AndroidManifest.xml and insert the following elements into the section:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto" package="com.crossgeeks.firebasepushnotificationsample" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="22" android:targetSdkVersion="28" />
<application android:label="FirebasePushSample.Android" android:icon="@drawable/icon">
<receiver
android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver"
android:exported="false" />  
</application>
<receiver
  android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
  android:exported="true"
  android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
  <action android:name="com.google.android.c2dm.intent.RECEIVE" />
  <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
  <category android:name="${applicationId}" />
</intent-filter>
</receiver>
</manifest>

然后您可以重试.

这是您可以看一下的示例:

This is sample that you can take a look:

https://github.com/CrossGeeks/FirebasePushNotificationPlugin

但是您仍然需要在AndroidManifest.xml中添加以上代码,然后运行此示例,您将成功获得

But you still need to add the code above in AndroidManifest.xml, and run this sample,you will get successfully.

这篇关于收到通知Xamarin表单时运行Sqlite代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆