问题描述
去年我在 Xamarin Forms 应用程序中配置了推送通知,本周决定更新到新的 SDK,在 iOS 上这一切顺利,但在 Android 上我遇到了问题。
我已尝试在此处复制文档:https://docs.microsoft.com/en-us/azure/notification-hubs/xamarin-notification-hubs-push-notifications-android-gcm 并相信我已经完成了 90% 的工作,因为我的应用在前台和后台时会很高兴收到推送通知,但我的应用无法接收推送当它被关闭(不是强制关闭)时的通知。我知道这 100% 不是设备问题,因为我使用 FirebaseService 类测试了旧配置并且它工作正常。我也在使用“数据”通知。
发送推送通知时,我能够在设备日志中找到以下错误消息“错误 (6621)/AndroidRuntime:java.lang.NullPointerException:尝试调用接口方法 'void com.microsoft. windowsazure.messaging.notificationhubs.NotificationListener.onPushNotificationReceived(android.content.Context,com.microsoft.windowsazure.messaging.notificationhubs.NotificationMessage)'在一个空对象引用上"
请参阅下面我的 MainActivity 类的代码。
[Activity(Label = "AndroidNotificationTest",Icon = "@mipmap/icon",Theme = "@style/MainTheme",MainLauncher = true,LaunchMode = LaunchMode.SingleTop,ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
internal static readonly string CHANNEL_ID = "my_notification_channel";
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
// Listen for push notifications
NotificationHub.SetListener(new AzureListener());
// Start the SDK
NotificationHub.Start(this.Application,Constants.NotificationHubName,Constants.ListenConnectionString);
NotificationHub.AddTag("Developer");
Xamarin.Essentials.Platform.Init(this,savedInstanceState);
global::Xamarin.Forms.Forms.Init(this,savedInstanceState);
LoadApplication(new App());
CreateNotificationChannel();
}
public override void OnRequestPermissionsResult(int requestCode,string[] permissions,[GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode,permissions,grantResults);
base.OnRequestPermissionsResult(requestCode,grantResults);
}
void CreateNotificationChannel()
{
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
return;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,"FCM Notifications",NotificationImportance.Default)
{
Description = "Firebase Cloud Messages appear in this channel"
};
NotificationManager notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
}
}
internal class AzureListener : Java.Lang.Object,INotificationListener
{
public void OnPushNotificationReceived(Context context,INotificationMessage message)
{
Console.WriteLine($"Message received with title {message.Title} and body {message.Body}");
}
}
还有我的 Android 清单文件。
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.package.appname">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="29" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<application android:label="AndroidNotificationTest.Android" android:theme="@style/MainTheme"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
我已经为此花了几天时间,但现阶段我对问题所在感到困惑。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)