1
votes

I'm trying to create registration using REST API NotificationHub AZURE from PHP API Microsoft

¿Anyone know how it's done?

Regards and thanks!

2

2 Answers

0
votes

Generally speaking, there are 3 main steps required to access Notification Hubs REST endpoints:

  1. Parse the connection string
  2. Generate the authorization token
  3. Perform the HTTP call

You can refer to https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-php-backend-how-to/ for more detail.

Meanwhile, you can directly use these PHP sample provided by Azure Team in your application, which can implement your requirements easily.

0
votes
# build uri
    $uri = $this->endpoint . $this->hubPath . "/registrations" . NotificationHub::API_NEW_VERSION;
    $ch = curl_init();

    $token = $this->generateSasToken($uri);

    $headers = [
        'Authorization: '. $token,
        'Content-Type: application/xml',
        'x-ms-version: 2015-01'
    ];

    $request_body = self::requestBodyRegistration($device_type, $tagsOrTagExpression, $device_code );

    if( is_null( $request_body ) )
    {
        return null;
    }

    curl_setopt_array($ch, array(
        CURLOPT_URL => $uri,
        CURLOPT_POST => TRUE,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_SSL_VERIFYPEER => FALSE,
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_POSTFIELDS => $request_body
    ));

    // Send the request
    $response = curl_exec($ch);

    // Check for errors
    if($response === FALSE){
        throw new Exception(curl_error($ch));
    }

    $info = curl_getinfo($ch);
    curl_close($ch);

private function requestBodyRegistration($device_type, $tagsOrTagExpression, $device_code )
{
    switch ($device_type) {
        case 'apple':
            return '<?xml version="1.0" encoding="utf-8"?>
                        <entry xmlns="http://www.w3.org/2005/Atom">
                            <content type="application/xml">
                                <AppleRegistrationDescription xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/netservices/2010/10/servicebus/connect">
                                    <Tags>'. $tagsOrTagExpression .'</Tags>
                                    <DeviceToken>'. $device_code .'</DeviceToken>
                                </AppleRegistrationDescription>
                            </content>
                        </entry>';
        case 'gcm':
            return '<?xml version="1.0" encoding="utf-8"?>
                                <entry xmlns="http://www.w3.org/2005/Atom">
                                    <content type="application/xml">
                                        <GcmRegistrationDescription xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/netservices/2010/10/servicebus/connect">
                                                <Tags>'. $tagsOrTagExpression .'</Tags>
                                                <GcmRegistrationId>'. $device_code .'</GcmRegistrationId>
                                        </GcmRegistrationDescription>
                                    </content>
                                </entry>';
        default:
            return null;
    }
}