Я реализовал IPushNotificationListener
, в моем классе CrossPushNotificationListener
. Как указано в файле README.Как реализовать плагин CrossPushNotification в форматах Xamarin?
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;
}
}
В AppDelegate приложения iOS я инициализирую плагин 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();
Я знаю, что этот метод выполняется, как я получаю предупреждение с запросом разрешения. Но ни один из методов интерфейса IPushNotificationListener
, реализованный в CrossPushNotificationListener
, не вызывается.
Что мне здесь не хватает?
спасибо.
Какие услуги вы используете для доставки уведомления толчка? Прежде всего, убедитесь, что устройство зарегистрировано правильно, а push-уведомление доставляется в службу Apple Push Notification. – hankide
Методы из CrossPushNotificationListener никогда не вызываются, поэтому я не получаю токен устройства.Также нет методов из AppDelegate. Регистрация происходит, когда я получаю запрос на получение предупреждений о разрешении. Этот бит меня сбивает с толку еще больше ... – nmdias