Xamarin 表单 - Android Firebase 通知从未显示

问题描述 投票:0回答:2

在 xamarin 表单项目中,我已将 Firebase 插件 添加到我的项目中以执行推送通知。

该插件在我的 IOS 项目中运行良好。我可以在输出中看到数据,当应用程序在后台时,会弹出通知。

我的问题: 在我的 Android 项目中,通知永远不会弹出。我可以在输出控制台中看到在 Firebase 平台上发送的数据,但是当应用程序处于后台时,不会出现弹出窗口,并且当我将值 com.AppName.urgent 放入 Strings.xml 文件中时出现此错误

[FirebaseMessaging] Notification Channel set in AndroidManifest.xml has not been created by the app. Default value will be used.
当我输入值

default时没有错误,但通知仍然没有出现

如何让通知出现在 Android 上?

这是我的代码:

我的 Android 项目中的 strings.xml 文件:

<?xml version="1.0" encoding="UTF-8" ?> <resources> <string name="notification_channel_id">default</string> </resources>
我的 Android 文件:MainActivity

namespace AppName.Droid { [Activity(Label = "AppName", Icon = "@mipmap/icon", Theme = "@style/MainTheme", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Orientation, ScreenOrientation = ScreenOrientation.Portrait )] public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity { internal static MainActivity Instance { get; private set; } protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); Xamarin.Essentials.Platform.Init(this, savedInstanceState); global::Xamarin.Forms.Forms.Init(this, savedInstanceState); LoadApplication(new App()); FirebasePushNotificationManager.ProcessIntent(this, Intent); } protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) { base.OnActivityResult(requestCode, resultCode, data); Stormlion.ImageCropper.Droid.Platform.OnActivityResult(requestCode, resultCode, data); } public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults) { Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults); base.OnRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case AndroidMicrophoneService.RecordAudioPermissionCode: if (grantResults[0] == Permission.Granted) { micService.OnRequestPermissionResult(true); } else { micService.OnRequestPermissionResult(false); } break; } } } }
我的 Firebase 文件:MainApplication

[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. #if DEBUG FirebasePushNotificationManager.Initialize(this, true); #else FirebasePushNotificationManager.Initialize(this,false); #endif //Handle notification when app is closed here CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) => { }; } }
在我的 app.xaml.cs 中:

public App() { InitializeComponent(); MainPage = new LInPage(); } // Firebase Notifcation CrossFirebasePushNotification.Current.OnTokenRefresh += (s, p) => { System.Diagnostics.Debug.WriteLine($"MyTOKEN : {p.Token}"); }; CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) => { System.Diagnostics.Debug.WriteLine("Received"); foreach (var data in p.Data) { System.Diagnostics.Debug.WriteLine($"Mydata {data.Key} : {data.Value}"); } }; CrossFirebasePushNotification.Current.OnNotificationOpened += (s, p) => { System.Diagnostics.Debug.WriteLine("Opened"); foreach (var data in p.Data) { System.Diagnostics.Debug.WriteLine($"Opended {data.Key} : {data.Value}"); } }; }
SplashActivity.cs(打开屏幕图像):

namespace AppName.Droid { [Activity(Theme = "@style/MyTheme.Splash", MainLauncher = true, NoHistory = true)] public class SplashActivity : Activity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Create your application here } protected override async void OnResume() { base.OnResume(); await SimulateStartup(); } async Task SimulateStartup() { await Task.Delay(1000); // Simulate a bit of startup work. StartActivity(new Intent(Application.Context, typeof(MainActivity))); } } }
我的 AndroidManifest.xml :

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionName="98.0" package="com.AppName" android:versionCode="98"> <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="31" /> <application android:label="AppName" android:theme="@style/MainTheme" android:icon="@mipmap/icon_round"> <activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity" android:theme="@style/Base.Theme.AppCompat" /> <meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/notification_channel_id" /> <!-- optional (needed if default theme has no action bar) --> <service android:name="crc6494e14b9856016c30.PNFirebaseMessagingService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service> </application> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-feature android:name="android.hardware.location" android:required="false" /> <uses-feature android:name="android.hardware.location.gps" android:required="false" /> <uses-feature android:name="android.hardware.location.network" android:required="false" /> <queries> <intent> <action android:name="android.intent.action.SENDTO" /> <data android:scheme="mailto" /> </intent> </queries> </manifest>
我已关注这些链接,但没有成功:

  • W/FirebaseMessaging:AndroidManifest 中缺少默认通知通道元数据。将使用默认值
android firebase xamarin.forms xamarin.android
2个回答
0
投票
您应该避免混淆声明性属性并手动编写 AndroidManifest.xml。

[Activity (Label = "MyApp", MainLauncher = true, Icon = "@mipmap/ic_launcher")]

正在实际使用(生成)
AndroidManifest.xml
中生成一段代码,如下所示:

<activity android:icon="@drawable/icon" android:label="AndroidApp1" android:name="md5c178831cd46fc56bebc42cf953f78ced.MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
你的代码将在其他地方。您可以在输出文件夹

AndroidManifest.xml

中找到生成的
.\obj\Debug\android

因此,尝试从清单文件中删除手动编辑。

更多信息,您可以查看文档:

Working with the Android Manifest

此外,您还可以参考这里的FirebasePushNotificationPlugin示例,它可以指导我们具体如何实现这个功能。


0
投票
[FirebaseMessaging] 应用尚未创建 AndroidManifest.xml 中设置的通知通道。将使用默认值。

错误消息试图说明您在清单(您提供了“默认”)和您的编程(您提供了“常规”)中指定了不同的通知通道 ID。您应该在两个地方使用相同的值。

试试这个:

FirebasePushNotificationManager.DefaultNotificationChannelId = "default";
    
© www.soinside.com 2019 - 2024. All rights reserved.