我已经在我的CrossPushNotificationListener
类中实施了IPushNotificationListener
。正如README文件中所建议的那样。如何在Xamarin Forms中实现CrossPushNotification插件?
public class CrossPushNotificationListener : IPushNotificationListener
{
void IPushNotificationListener.OnError(string message, DeviceType deviceType)
{
Application.Current.MainPage.DisplayAlert("error", message, "ok");
}
void IPushNotificationListener.OnMessage(JObject values, DeviceType deviceType)
{
Application.Current.MainPage.DisplayAlert("message", values.ToString(), "ok");
}
void IPushNotificationListener.OnRegistered(string token, DeviceType deviceType)
{
Application.Current.MainPage.DisplayAlert("token", token, "ok");
}
void IPushNotificationListener.OnUnregistered(DeviceType deviceType)
{
Application.Current.MainPage.DisplayAlert("unregistered", "", "ok");
}
bool IPushNotificationListener.ShouldShowNotification()
{
Application.Current.MainPage.DisplayAlert("should show notification", "", "ok");
return true;
}
}
在iOS的AppDelegate中,我初始化了CrossPushNotification插件。
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
CrossPushNotification.Initialize<CrossPushNotificationListener>();
LoadApplication(new Origination.App());
return base.FinishedLaunching(app, options);
}
我也用适当的覆盖延伸的AppDelegate如图所示PushNotificationApplicationDelegate.txt.pp
文件:
public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
{
if (CrossPushNotification.Current is IPushNotificationHandler)
{
((IPushNotificationHandler)CrossPushNotification.Current).OnErrorReceived(error);
}
}
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
if (CrossPushNotification.Current is IPushNotificationHandler)
{
((IPushNotificationHandler)CrossPushNotification.Current).OnRegisteredSuccess(deviceToken);
}
}
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
if (CrossPushNotification.Current is IPushNotificationHandler)
{
((IPushNotificationHandler)CrossPushNotification.Current).OnMessageReceived(userInfo);
}
}
public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
{
if (CrossPushNotification.Current is IPushNotificationHandler)
{
((IPushNotificationHandler)CrossPushNotification.Current).OnMessageReceived(userInfo);
}
}
后来,在我的共享代码,用户注册后/登录的应用程序,然后输入它的主屏幕,我打电话:
CrossPushNotification.Current.Register();
我知道这个方法正在执行,因为我得到了一个警告请求权限。但是,调用CrossPushNotificationListener
中实现的IPushNotificationListener
接口中的方法都不是。
我在这里错过了什么?
谢谢。
您使用的推送通知传递什么样的服务?您应该首先检查设备是否已正确注册,并将推送通知传送到Apple推送通知服务。 – hankide
CrossPushNotificationListener中的方法永远不会被调用,所以我没有获取设备令牌。AppDelegate中的方法也不是。注册发生,因为我收到请求权限警报。这一点让我更加困惑...... – nmdias