Verwenden von Notification Hubs von PHP aus

Sie können wie im MSDN-Thema REST-APIs für Notification Hubs beschrieben mithilfe der Notification Hubs-REST-Schnittstelle über ein Java-, PHP- oder Ruby-Back-End auf alle Features von Notification Hubs zugreifen.

In diesem Thema wird Folgendes erläutert:

Clientschnittstelle

Die Hauptschnittstelle des Clients kann dieselben Methoden bereitstellen, die im .NET Notification Hubs SDK verfügbar sind. Damit können Sie alle Tutorials und Beispiele, die aktuell auf dieser Website erhältlich sind und von der Community im Internet beigetragen werden, direkt umsetzen.

Sie finden den gesamten Code im PHP REST-Wrapper-Beispiel.

So erstellen Sie beispielsweise einen Client:

$hub = new NotificationHub("connection string", "hubname");

So senden Sie eine native iOS-Benachrichtigung:

$notification = new Notification("apple", '{"aps":{"alert": "Hello!"}}');
$hub->sendNotification($notification, null);

Implementierung

Führen Sie, sofern nicht bereits geschehen, das Tutorial zu den ersten Schritten bis zum letzten Abschnitt aus, in dem Sie das Back-End implementieren müssen. Sie können auch den Code aus dem PHP REST-Wrapper-Beispiel verwenden und direkt mit dem Abschnitt Abschließen des Tutorials fortfahren.

Alle Details für das Implementieren eines vollständigen REST-Wrappers finden sich auf MSDN. In diesem Abschnitt beschreiben wir die PHP-Implementierung der Hauptschritte, die für den Zugriff auf REST-Endpunkte von Notification Hubs erforderlich sind:

  1. Analysieren der Verbindungszeichenfolge
  2. Generieren des Authentifizierungstokens
  3. Durchführen des HTTP-Aufrufs

Analysieren der Verbindungszeichenfolge

Hier ist die Hauptklasse, die den Client implementiert, dessen Konstruktor die Verbindungszeichenfolge analysiert:

class NotificationHub {
    const API_VERSION = "?api-version=2013-10";

    private $endpoint;
    private $hubPath;
    private $sasKeyName;
    private $sasKeyValue;

    function __construct($connectionString, $hubPath) {
        $this->hubPath = $hubPath;

        $this->parseConnectionString($connectionString);
    }

    private function parseConnectionString($connectionString) {
        $parts = explode(";", $connectionString);
        if (sizeof($parts) != 3) {
            throw new Exception("Error parsing connection string: " . $connectionString);
        }

        foreach ($parts as $part) {
            if (strpos($part, "Endpoint") === 0) {
                $this->endpoint = "https" . substr($part, 11);
            } else if (strpos($part, "SharedAccessKeyName") === 0) {
                $this->sasKeyName = substr($part, 20);
            } else if (strpos($part, "SharedAccessKey") === 0) {
                $this->sasKeyValue = substr($part, 16);
            }
        }
    }
}

Erstellen eines Sicherheitstokens

Informationen zum Erstellen eines SAS-Sicherheitstokens finden Sie in der Azure-Dokumentation.

Fügen Sie der NotificationHub-Klasse die generateSasToken-Methode hinzu, um das Token basierend auf dem URI der aktuellen Anforderung und den aus der Verbindungszeichenfolge extrahierten Anmeldeinformationen zu erstellen.

private function generateSasToken($uri) {
    $targetUri = strtolower(rawurlencode(strtolower($uri)));

    $expires = time();
    $expiresInMins = 60;
    $expires = $expires + $expiresInMins * 60;
    $toSign = $targetUri . "\n" . $expires;

    $signature = rawurlencode(base64_encode(hash_hmac('sha256', $toSign, $this->sasKeyValue, TRUE)));

    $token = "SharedAccessSignature sr=" . $targetUri . "&sig="
                . $signature . "&se=" . $expires . "&skn=" . $this->sasKeyName;

    return $token;
}

Senden einer Benachrichtigung

Zuerst definieren wir eine Klasse, die eine Benachrichtigung darstellt.

class Notification {
    public $format;
    public $payload;

    # array with keynames for headers
    # Note: Some headers are mandatory: Windows: X-WNS-Type, WindowsPhone: X-NotificationType
    # Note: For Apple you can set Expiry with header: ServiceBusNotification-ApnsExpiry in W3C DTF, YYYY-MM-DDThh:mmTZD (for example, 1997-07-16T19:20+01:00).
    public $headers;

    function __construct($format, $payload) {
        if (!in_array($format, ["template", "apple", "windows", "fcm", "windowsphone"])) {
            throw new Exception('Invalid format: ' . $format);
        }

        $this->format = $format;
        $this->payload = $payload;
    }
}

Diese Klasse ist ein Container für einen nativen Benachrichtigungstext oder ein Satz von Eigenschaften einer Benachrichtigungsvorlage sowie ein Satz von Headern, die ein Format (native Plattform oder Vorlage) und plattformspezifische Eigenschaften (wie die Apple-Ablaufeigenschaft und WNS-Header) enthalten.

Alle verfügbaren Optionen finden Sie in der Dokumentation der REST-APIs für Notification Hubs und unter den Formaten der einzelnen Benachrichtigungsplattformen.

Mit dieser Klasse können wir jetzt die Methoden zum Senden von Benachrichtigungen in der NotificationHub-Klasse schreiben:

public function sendNotification($notification, $tagsOrTagExpression="") {
    if (is_array($tagsOrTagExpression)) {
        $tagExpression = implode(" || ", $tagsOrTagExpression);
    } else {
        $tagExpression = $tagsOrTagExpression;
    }

    # build uri
    $uri = $this->endpoint . $this->hubPath . "/messages" . NotificationHub::API_VERSION;
    $ch = curl_init($uri);

    if (in_array($notification->format, ["template", "apple", "fcm"])) {
        $contentType = "application/json";
    } else {
        $contentType = "application/xml";
    }

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

    $headers = [
        'Authorization: '.$token,
        'Content-Type: '.$contentType,
        'ServiceBusNotification-Format: '.$notification->format
    ];

    if ("" !== $tagExpression) {
        $headers[] = 'ServiceBusNotification-Tags: '.$tagExpression;
    }

    # add headers for other platforms
    if (is_array($notification->headers)) {
        $headers = array_merge($headers, $notification->headers);
    }

    curl_setopt_array($ch, array(
        CURLOPT_POST => TRUE,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_SSL_VERIFYPEER => FALSE,
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_POSTFIELDS => $notification->payload
    ));

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

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

    $info = curl_getinfo($ch);

    if ($info['http_code'] <> 201) {
        throw new Exception('Error sending notification: '. $info['http_code'] . ' msg: ' . $response);
    }
} 

Die obigen Methoden senden eine HTTP-POST-Anforderung mit dem korrekten Text und Headern an den /messages-Endpunkt des Notification Hub, um die Benachrichtigung zu senden.

Tutorial abschließen

Sie können jetzt das Tutorial zu den ersten Schritten abschließen, indem Sie die Benachrichtigung von einem PHP-Back-End senden.

Initialisieren Sie Ihren Notification Hubs-Client (ersetzen Sie die Verbindungszeichenfolge und den Hubnamen wie im [Tutorial zu den ersten Schritten] beschrieben):

$hub = new NotificationHub("connection string", "hubname");

Fügen Sie dann den Sendecode je nach mobiler Zielplattform hinzu.

Windows Store und Windows Phone 8.1 (nicht Silverlight)

$toast = '<toast><visual><binding template="ToastText01"><text id="1">Hello from PHP!</text></binding></visual></toast>';
$notification = new Notification("windows", $toast);
$notification->headers[] = 'X-WNS-Type: wns/toast';
$hub->sendNotification($notification, null);

iOS

$alert = '{"aps":{"alert":"Hello from PHP!"}}';
$notification = new Notification("apple", $alert);
$hub->sendNotification($notification, null);

Android

$message = '{"data":{"msg":"Hello from PHP!"}}';
$notification = new Notification("fcm", $message);
$hub->sendNotification($notification, null);

Windows Phone 8.0 und 8.1 Silverlight

$toast = '<?xml version="1.0" encoding="utf-8"?>' .
            '<wp:Notification xmlns:wp="WPNotification">' .
               '<wp:Toast>' .
                    '<wp:Text1>Hello from PHP!</wp:Text1>' .
               '</wp:Toast> ' .
            '</wp:Notification>';
$notification = new Notification("windowsphone", $toast);
$notification->headers[] = 'X-WindowsPhone-Target : toast';
$notification->headers[] = 'X-NotificationClass : 2';
$hub->sendNotification($notification, null);

Kindle Fire

$message = '{"data":{"msg":"Hello from PHP!"}}';
$notification = new Notification("adm", $message);
$hub->sendNotification($notification, null);

Beim Ausführen des PHP-Codes sollte jetzt eine Benachrichtigung erstellt werden, die auf dem Zielgerät angezeigt wird.

Nächste Schritte

In diesem Thema wurde gezeigt, wie Sie einen einfachen PHP-REST-Client für Notification Hubs erstellen. Hier können Sie folgende Aktionen ausführen:

  • Laden Sie das vollständige PHP REST-Wrapper-Beispielherunter, das den gesamten vorstehenden Code enthält.
  • Erfahren Sie mehr über das Tagging-Feature von Notification Hubs im [Breaking News tutorial]
  • Lernen Sie im [Benachrichtigen von Benutzern-Lernprogramm], wie Sie Pushbenachrichtigungen an einzelne Benutzer senden.

Weitere Informationen finden Sie außerdem im PHP Developer Center.

Senden von Pushbenachrichtigungen an iOS-Apps mit Azure Notification Hubs