在一个xamarin表单项目中,我已经将Firebase plugin添加到我的项目中来做推送通知。
该插件在我的IOS项目中运行良好。我可以在输出中看到数据,当应用程序在后台时,通知弹出。
我的问题:在我的Android项目中,通知永远不会弹出。我可以在输出控制台中看到Firebase平台上发送的数据,但当应用程序处于后台时,弹出窗口不会出现,当我将值com.AppName. ururge放入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文件:主要活动
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文件:主应用程序
[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>
型
我试过这些链接,但没有成功:
1条答案
按热度按时间bxjv4tth1#
您应该避免混淆声明性属性和手动编写AndroidManifest.xml。
[Activity (Label = "MyApp", MainLauncher = true, Icon = "@mipmap/ic_launcher")]
在实际使用(生成)的AndroidManifest.xml
中生成一段代码,看起来像:字符串
你的代码会在别的地方您可以在输出文件夹
.\obj\Debug\android
中找到生成的AndroidManifest.xml
。因此,请尝试从清单文件中删除手动编辑。
有关详细信息,您可以查看文档:Working with the Android Manifest的数据。
除外,您还可以参考FirebasePushNotificationPlugin here示例,该示例可以指导我们如何具体实现此功能。