Search code examples
phpiosazurepush-notificationazure-notificationhub

How to Send Push Notification to a specific user on Notification Hub?


Is there any way where I can send a notification to a specific user? I tried to use the deviceToken. But I couldn't find any tutorial that shows a way where I can send notifications to a devicetoken.


Solution

  • According the description on https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-node-backend-how-to-use-server-sdk/#push-user:

    When an authenticated user registers for push notifications, a user ID tag is automatically added to the registration. By using this tag, you can send push notifications to all devices registered by a specific user.

    You can create an EasyTable script associate to a table to store your authenticated user's ID. You can leverage following code snippet to implement this:

    table.insert(function (context) {
        context.item.userId = context.user.id;
        return context.execute();
    });
    

    Refer to https://github.com/Azure/azure-content/blob/master/articles/app-service-mobile/app-service-mobile-node-backend-how-to-use-server-sdk.md#how-to-adjust-the-query-that-is-used-with-table-operations for more.

    Then use this PHP sample to send notification with the user id:

    $hub = new NotificationHub("<connectionString>", "<hubName>");
    $message = '{"data":{"message":"Hello from PHP!"}}';
    $notification = new Notification("gcm", $message);
    $hub->sendNotification($notification, "_UserId:sid:<UserId>");