當我的 Xamarin 應用程式關閉時,我無法使用 Plugin.FirebasePushNotification 顯示推送通知。
這是我到目前為止所做的:
- 添加了 google-services.json 并將其構建操作設定為 GoogleServiceJson
MainActivity.cs
//some dependencies
using Plugin.FirebasePushNotification;
using System.Collections.Specialized;
[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace MasterDetailPageNavigation.Droid
{
[Activity(Label = "app", Theme = "@style/MyTheme.Splash", MainLauncher = true, ScreenOrientation = ScreenOrientation.Portrait)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
const string TAG = "MainActivity";
internal static readonly string CHANNEL_ID = "my_notification_channel";
internal static readonly int NOTIFICATION_ID = 100;
protected override void OnCreate(Bundle bundle)
{
base.Window.RequestFeature(WindowFeatures.ActionBar);
base.SetTheme(Resource.Style.MainTheme);
base.OnCreate(bundle);
Xamarin.Essentials.Platform.Init(this, bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App(Param));
FirebasePushNotificationManager.ProcessIntent(this, Intent);
CrossFirebasePushNotification.Current.OnTokenRefresh = Current_OnTokenRefresh;
}
private void Current_OnTokenRefresh(object source, FirebasePushNotificationTokenEventArgs e)
{
System.Diagnostics.Debug.WriteLine("NEW TOKEN => " e.Token);
App.TokenNotification = e.Token;
Task.Run(() => SendRegistrationToServer(e.Token));
}
應用程式.cs
using System;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Support.V4.App;
using Plugin.FirebasePushNotification;
using static Android.Support.V4.App.NotificationCompat;
namespace MasterDetailPageNavigation.Droid
{
[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 = "FirebaseDefaultChannel";
//Change for your default notification channel name here
FirebasePushNotificationManager.DefaultNotificationChannelName = "General";
FirebasePushNotificationManager.DefaultNotificationChannelImportance = NotificationImportance.Max;
}
//If debug you should reset the token each time.
#if DEBUG
FirebasePushNotificationManager.Initialize(this,true);
#else
FirebasePushNotificationManager.Initialize(this, false);
#endif
//Handle notification when app is closed here
CrossFirebasePushNotification.Current.OnNotificationReceived = (s, p) =>
{
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
foreach (var key in p.Data.Keys)
{
Console.WriteLine("String key: " key " = " p.Data[key].ToString());
intent.PutExtra(key, p.Data[key].ToString());
}
p.Data.TryGetValue("title", out object Title);
p.Data.TryGetValue("body", out object Body);
var pendingIntent = PendingIntent.GetActivity(this, MainActivity.NOTIFICATION_ID, intent, PendingIntentFlags.UpdateCurrent);
var notificationBuilder = new NotificationCompat.Builder(this, MainActivity.CHANNEL_ID)
.SetSmallIcon(Resource.Drawable.ic_launcher_round)
.SetContentTitle(Title.ToString())
.SetContentText(Body.ToString())
.SetAutoCancel(true)
.SetContentIntent(pendingIntent)
.SetPriority(PriorityHigh);
//.SetStyle(new BigPictureStyle().SetSummaryText(Body.ToString()))
var notificationManager = NotificationManagerCompat.From(this);
notificationManager.Notify(MainActivity.NOTIFICATION_ID, notificationBuilder.Build());
};
}
}
}
AndroidManifest.xml(我想知道這是否還有必要,出于測驗目的,我已將其注釋掉,并且行為完全相同,但這是我的應用程式的舊版本)
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
<receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" />
<receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:enabled="true" 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>
當前行為:
當應用程式處于前臺和后臺時,我可以收到訊息,但是當我關閉應用程式時沒有傳遞訊息。如果我從方法“CrossFirebasePushNotification.Current.OnNotificationReceived”中注釋掉代碼,一切都會停止作業(我已經讀過它只對后臺模式是必要的,但前臺似乎也在使用它)。我幾乎要降級以使事情再次正常進行。
這里有沒有人看到我在這里做錯了什么?
PS:我正在使用 firebase 控制臺發送測驗訊息。
uj5u.com熱心網友回復:
剛剛在插件 FireBasePushNotification 的專案頁面找到了答案:
他們的FAQ的第三點:
“3 - 如果應用程式在除錯時停止,您將不會收到任何推送通知,應在應用程式關閉時重新打開并關閉通知才能作業。這是由于在除錯時停止時應用程式處于不穩定狀態。”
因此,我所做的一切都是關閉并僅使用手機重新打開應用程式(而不是通過 Visual Studio 除錯)和賓果游戲,現在它可以作業了!
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/432721.html
標籤:安卓 xamarin 推送通知 firebase-云消息传递
