Call method in xamarin.ios from PCL project

Victor86Victor86 Member ✭✭

In my Xamarin.iOS project in AppDelegate class I have method:

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
    Hub = new SBNotificationHub(Constants.ListenConnectionString, Constants.NotificationHubName);

    Hub.UnregisterAllAsync (deviceToken, (error) => {
        if (error != null)
        {
            System.Diagnostics.Debug.WriteLine("Error calling Unregister: {0}", error.ToString());
            return;
        }

        NSSet tags = null; // create tags if you want
        Hub.RegisterNativeAsync(deviceToken, tags, (errorCallback) => {
            if (errorCallback != null)
                System.Diagnostics.Debug.WriteLine("RegisterNativeAsync error: " + errorCallback.ToString());
        });
    });
}

I am trying to call this method from PCL project. My attempts to do it by using DependencyService were unlucky and I didn't find examples how I can do it correctly. Hope for some help.

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Two things:

    1 - PCL is near dead. Its past end of life. You wan to make a .NET Standard solution.

    I am trying to call this method from PCL project.

    Yeah - that's all sorts of wrong way of thing about this stuff.

    2 - Your agnostic layer doesn't know what OS its running on. You're not going to call iOS methods. That whole approach is wrong.

    You most likely want a DependencyService and then the iOS or android version will call its own version of a given method.

    My attempts to do it by using DependencyService were unlucky

    Well... That doesn't mean give up and do something else. You need to keep working on doing things the right way.

  • ColeXColeX Member, Xamarin Team Xamurai
    edited March 6

    I am trying to call this method from PCL project.

    It is pretty easy to use DependencyService or Messaging center to call normal method from PCL project(as Clint said, you should use .NET Standard instead now).

    However , RegisteredForRemoteNotifications is triggered by Apple Push Notification service, we should not call it manually , since deviceToken can't be access when calling it .

Sign In or Register to comment.