2017-02-16 6 views
1

Я реализовал 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, не вызывается.

Что мне здесь не хватает?

спасибо.

+0

Какие услуги вы используете для доставки уведомления толчка? Прежде всего, убедитесь, что устройство зарегистрировано правильно, а push-уведомление доставляется в службу Apple Push Notification. – hankide

+0

Методы из CrossPushNotificationListener никогда не вызываются, поэтому я не получаю токен устройства.Также нет методов из AppDelegate. Регистрация происходит, когда я получаю запрос на получение предупреждений о разрешении. Этот бит меня сбивает с толку еще больше ... – nmdias

ответ

0

КСН вы должны попытаться назвать:

CrossPushNotification.Initialize<CrossPushNotificationListener>(); 

после

LoadApplication(new Origination.App()); 

Как это:

 public override bool FinishedLaunching(UIApplication app, NSDictionary options) 
    { 
     global::Xamarin.Forms.Forms.Init(); 

     LoadApplication(new App()); 

     CrossPushNotification.Initialize<CrossPushNotificationListener>(); 
     return base.FinishedLaunching(app, options); 
    } 
+0

Не это. Я закончил создание службы зависимостей с аналогичной реализацией плагина, и это сработало. Благодарю. – nmdias

+1

Можно ли поделиться своей реализацией и выбрать ее в качестве ответа на свой вопрос, интересно узнать, как вы об этом поработали с помощью службы зависимостей? –

1

Выборочная реализации с DependencyService. Этот код основан на решении, предоставляемом плагином Xam.Plugin.PushNotification.

Shared проекта

Интерфейсы

public interface INotificationListener 
{ 
    void OnRegister(string deviceToken); 
    void OnMessage(JObject values); 
} 

public interface INotificationService 
{ 
    string Token { get; } 
    void Register(); 
} 

Реализация

public class NotificationListener : INotificationListener 
{ 
    public void OnMessage(JObject values) 
    { 
     // TOOD: - Handle incoming notifications 
    } 

    public async void OnRegister(string deviceToken) 
    { 
     // TODO: - Register the devices token in the server 
    } 
} 

public class NotificationManager 
{ 

    private static NotificationManager _current = null; 

    /// <summary> 
    /// Shared instance of the Notification Manager 
    /// </summary> 
    public static NotificationManager Current 
    { 
     get 
     { 
      if (_current == null) 
      { 
       _current = new NotificationManager(); 
      } 
      return _current; 
     } 
    } 

    /// <summary> 
    /// The member responsible for handling notifications 
    /// </summary> 
    public static INotificationListener Listener { get; private set; } 

    /// <summary> 
    /// Initializes the Notification Manager with an instance of the specified handler in type T 
    /// </summary> 
    /// <typeparam name="T"></typeparam> 
    public static void Initialize<T>() where T: INotificationListener, new() 
    { 
     Listener = new T(); 
    } 

} 

Вызов это всякий раз, когда это уместно спросить у пользователя разрешения

DependencyService.Get<INotificationService>().Register(); 

IOS проекта

[assembly: Xamarin.Forms.Dependency(typeof (NotificationService))] 
namespace App.iOS.Notifications 
{ 
    public class NotificationService : INotificationService 
    { 

     public string Token 
     { 
      get 
      { 
       return NSUserDefaults.StandardUserDefaults.StringForKey(NotificationKeys.TokenKey); 
      } 

     } 

     public void Register() 
     { 
      if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0)) 
      { 
       UIUserNotificationType notificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound; 
       var settings = UIUserNotificationSettings.GetSettingsForTypes(notificationTypes, new NSSet()); 
       UIApplication.SharedApplication.RegisterUserNotificationSettings(settings); 
       UIApplication.SharedApplication.RegisterForRemoteNotifications(); 
      } 
      else 
      { 
       UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound; 
       UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes); 
      } 
     } 

    } 

} 

App Делегат

public override bool FinishedLaunching(UIApplication app, NSDictionary options) 
{ 
    LoadApplication(new Origination.App()); 
    NotificationManager.Initialize<NotificationListener>(); 
    return base.FinishedLaunching(app, options); 
} 

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken) 
{ 
    // Get current device token 
    var DeviceToken = deviceToken.Description; 
    if (!string.IsNullOrWhiteSpace(DeviceToken)) 
    { 
     DeviceToken = DeviceToken.Trim('<').Trim('>').Replace(" ", ""); 
    } 

    // Get previous device token 
    var oldDeviceToken = NSUserDefaults.StandardUserDefaults.StringForKey(NotificationKeys.TokenKey); 

    // Has the token changed? 
    if (string.IsNullOrEmpty(oldDeviceToken) || !oldDeviceToken.Equals(DeviceToken)) 
    { 
     NotificationManager.Listener.OnRegister(DeviceToken); 
    } 

    // Save new device token 
    NSUserDefaults.StandardUserDefaults.SetString(DeviceToken, NotificationKeys.TokenKey); 
    NSUserDefaults.StandardUserDefaults.Synchronize(); 

} 

public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler) 
{ 
    HandleNotification(userInfo); 
} 

public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo) 
{ 
    HandleNotification(userInfo); 
} 

#region Handle Notification 

private static string DictionaryToJson(NSDictionary dictionary) 
{ 
    NSError error; 
    var json = NSJsonSerialization.Serialize(dictionary, NSJsonWritingOptions.PrettyPrinted, out error); 
    return json.ToString(NSStringEncoding.UTF8); 
} 

public void HandleNotification(NSDictionary userInfo) 
{ 
    var parameters = new Dictionary<string, object>(); 
    var json = DictionaryToJson(userInfo); 
    JObject values = JObject.Parse(json); 

    var keyAps = new NSString("aps"); 

    if (userInfo.ContainsKey(keyAps)) 
    { 
     NSDictionary aps = userInfo.ValueForKey(keyAps) as NSDictionary; 

     if (aps != null) 
     { 
      foreach (var apsKey in aps) 
      { 
       parameters.Add(apsKey.Key.ToString(), apsKey.Value); 
       JToken temp; 
       if (!values.TryGetValue(apsKey.Key.ToString(), out temp)) 
        values.Add(apsKey.Key.ToString(), apsKey.Value.ToString()); 
      } 
     } 
    } 

    NotificationManager.Listener.OnMessage(values); 
} 

#endregion 
Смежные вопросы