Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve a PNS Handle from the Platform Notification Service?

I have an Xamarin.iOS application that is connecting an Azure backend service and I want my service to send notifications to the client applications.

The Microsoft documentation explains how to set up the Notification Hub for different scenario. I think I am getting most of it, however I am not sure I understood the very first part, which is for the iOS application to Retrieve PNS Handle from the Platform Notification Service, as shown in the following picture:

enter image description here

It looks like this is some task that must be performed by the client application alone and then communicate this to the backend service for the registration.

I have a feeling that it happens at the 10th step of this section, when iOS calls the application back on the method RegisteredForRemoteNotifications. In that callback, the application is given a deviceToken:

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());
        });
    });
}

Question

Is that deviceToken the PNS Handle that I need to send to the backend service to start the registration process? If not, how am I supposed to contact the PNS to get the Handle?

like image 431
Kzrystof Avatar asked Oct 30 '25 23:10

Kzrystof


2 Answers

The information is in the documentation but not in an obvious form for a C# developer.

In Objective-C, the deviceToken is provided by the iOS application, as mentioned by @LucasZ, after it got registered against the PNS.

However I can't just send this deviceToken right away as it will not be accepted by the AppleRegistrationDescription class used in my Service.

It took me a while to get more familiar with Objective-C to figure out that this token was actually transformed before being sent to Azure:

NSSet* tagsSet = tags?tags:[[NSSet alloc] init];

NSString *deviceTokenString = [[token description]
        stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];

    deviceTokenString = [[deviceTokenString stringByReplacingOccurrencesOfString:@" " withString:@""] uppercaseString];

I have implemented something similar in C#:

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
    string pnsHandle = deviceToken.Description
                                  .Replace("<", string.Empty)
                                  .Replace(">", string.Empty)
                                  .Replace(" ", string.Empty)
                                  .ToUpper(); 

    Hub = new SBNotificationHub(Constants.ListenConnectionString, Constants.NotificationHubName);

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

        // In my use case, the tags are assigned by the server based on privileges.
        NSSet tags = null;

        Hub.RegisterNativeAsync(pnsHandle, tags, (errorCallback) => 
        {
            if (errorCallback != null)
                System.Diagnostics.Debug.WriteLine("RegisterNativeAsync error: " + errorCallback.ToString());
        });
    });
}

To answer my question, yes, the deviceToken is the PNS Handle but it must be formatted.

like image 117
Kzrystof Avatar answered Nov 02 '25 13:11

Kzrystof


The method RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken) is to tell the delegate that the app successfully registered with the Push Notification service.

The parameter ‘deviceToken’ is a globally unique token that identifies this device to the Push Notification service.

    NSSet tags = null; // create tags if you want

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

Because you are using Azure, the Hub has send the token to the generate remote notifications in above method. So if you only want to push some thing to all users, you don't need to do something else. If you want to push to specific users, you can register a tag and use it as parameter.

like image 20
Lucas Zhang Avatar answered Nov 02 '25 14:11

Lucas Zhang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!