Run Sqlite Code when received notification Xamarin Forms

不想你离开。 提交于 2020-12-14 23:32:47

问题


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" />

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);

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);
    }

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


回答1:


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>

Then you can try again.

This is sample that you can take a look:

https://github.com/CrossGeeks/FirebasePushNotificationPlugin

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



来源:https://stackoverflow.com/questions/60550771/run-sqlite-code-when-received-notification-xamarin-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!