【发布时间】:2017-02-16 10:57:02
【问题描述】:
我在CrossPushNotificationListener 类中实现了IPushNotificationListener。正如README 文件中所建议的那样。
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 Push Notification 服务。
-
CrossPushNotificationListener 中的方法永远不会被调用,所以我没有得到设备令牌。 AppDelegate 中的方法也不是。当我收到权限警报请求时,就会发生注册。这一点让我更加困惑......
标签: c# xamarin push-notification xamarin.ios xamarin.forms