创建注册通知中心Azure PHP

时间:2016-03-11 13:17:38

标签: php azure push-notification azure-notificationhub

我正在尝试使用来自PHP API Microsoft的REST API NotificationHub AZURE 创建注册

¿任何人都知道它是如何完成的?

问候并感谢!

2 个答案:

答案 0 :(得分:0)

一般来说,访问Notification Hubs REST端点需要3个主要步骤:

  1. 解析连接字符串
  2. 生成授权令牌
  3. 执行HTTP调用
  4. 您可以参考https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-php-backend-how-to/了解更多详情。

    同时,您可以直接在应用程序中使用Azure Team提供的these PHP sample,这可以轻松实现您的要求。

答案 1 :(得分:0)

# 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;
    }
}