我有一个Xamarin for Visual Studio跨平台项目,在尝试使用Android项目中的Azure Notifications Hub获取通知时出错。在FirebaseInstanceIdService-derived
类中,当我尝试实例化NotificationHub
对象时出现错误。调用此选项是为了在 Azure 通知中心注册用户/设备。
NotificationHub hub = new NotificationHub(Constants.NotificationHubName, Constants.ListenConnectionString, this);
我通常会收到此错误:
Unhandled Exception: Java.Lang.NullPointerException: occurred.
但也收到了:
Unhandled Exception: Java.Lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference occurred.
我从主页的 OnAppear 事件中调用的依赖项服务调用它。我不能依赖 FirebaseInstanceIdService 派生类中的 OnTokenRefresh((,因为我需要用户先登录。
我确认中心名称和连接字符串正确无误。与上下文相关的错误无疑使它似乎与此有关。但我不确定问题是什么,因为这是我看到的所有示例代码中的处理方式。
任何建议将不胜感激。
您是否在清单中插入了以下元素?
<receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" />
<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>
退房:
https://learn.microsoft.com/en-us/azure/app-service-mobile/app-service-mobile-xamarin-forms-get-started-push
https://learn.microsoft.com/en-us/azure/notification-hubs/xamarin-notification-hubs-push-notifications-android-gcm
附言。你能发布其他代码吗?确定问题可能很有用。
谢谢三笠。 这是方法:
//AuthenticatedUser is a class in the mobile app.
public void RegisterWithNotificationServer(AuthenticatedUser savedUser)
{
string token = FirebaseInstanceId.Instance.Token;
string customerNotificationTag = "customerid:" + savedUser.CustomerNotificationTagID;
string userNotificationTag = "userid:" + savedUser.UserNotificationTagID;
//Hub variable defined with class scope.
hub = new NotificationHub(Constants.NotificationHubName, Constants.ListenConnectionString, this);
List<string> tags = new List<string>() { customerNotificationTag, userNotificationTag };
Registration registration = hub.Register(token, tags.ToArray());
string regID = registration.RegistrationId;
}