CrossPushNotification OnNotificationReceived не вызывается в xamarin iOS - PullRequest
0 голосов
/ 17 октября 2018

Я попытался внедрить и запустить прослушиватель в моем приложении Xamarin для iOS.

Но, если мое приложение находится на переднем плане и получает сообщение облака базы Firebase, CrossPushNotification.Current.OnNotificationReceived не вызывается.

В чем проблема?

[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate, IUNUserNotificationCenterDelegate, IMessagingDelegate
{
    public override bool FinishedLaunching(UIApplication uiApplication, NSDictionary launchOptions)
    {
        global::Xamarin.Forms.Forms.Init();
        App.Configure();
        this.RegisterForRemoteNotifications(launchOptions);
        this.LoadApplication(new MyApp());
        return base.FinishedLaunching(uiApplication, launchOptions);
    }

    private void RegisterForRemoteNotifications(NSDictionary launchOptions)
    {
        PushNotificationManager.Initialize(launchOptions, true);
        CrossPushNotification.Current.RegisterForPushNotifications();
        Messaging.SharedInstance.ShouldEstablishDirectChannel = true;

        CrossPushNotification.Current.OnTokenRefresh += (s, p) =>
        {
            System.Diagnostics.Debug.WriteLine($"TOKEN : {p.Token}");
            Messaging.SharedInstance.ApnsToken = p.Token;
        };

        CrossPushNotification.Current.OnNotificationReceived += (s, p) =>
        {
            System.Diagnostics.Debug.WriteLine("Received");
        };
    }

    public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
    {
        PushNotificationManager.DidRegisterRemoteNotifications(deviceToken);
        Messaging.SharedInstance.ApnsToken = deviceToken;
    }

    // To receive notifications in background in any iOS version
    public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
    {
        PushNotificationManager.DidReceiveMessage(userInfo);
    }

    public override void DidEnterBackground(UIApplication uiApplication)
    {
        Messaging.SharedInstance.ShouldEstablishDirectChannel = false;
    }
}

1 Ответ

0 голосов
/ 18 октября 2018

Если вы используете Firebase. Вы должны использовать пакет Plugin.FirebasePushNotification из NuGet, а не Plugin.PushNotification.Измените метод следующим образом

CrossFirebasePushNotification.Current.OnTokenRefresh += (s,p) =>
  {
        System.Diagnostics.Debug.WriteLine($"TOKEN : {p.Token}");
  };

CrossFirebasePushNotification.Current.OnNotificationReceived += (s,p) =>
  {

        System.Diagnostics.Debug.WriteLine("Received");

  };

Для более подробной информации вы можете обратиться здесь

...