Azure 移动服务:向特定 android 设备发送推送通知

Azure mobile service : send push notification to specific android device

我正在尝试使用 Azure 移动服务发送推送通知。我让它工作,但目前它发送到所有使用应用程序密钥的设备。我意识到在 gcm.push.send() 函数中,如果我想将它发送给个人或团体,我必须提供一个标签而不是 null。

我只想发送给当前用户。调用插入的用户。我尝试输入用户 gcm 注册 ID,但这不起作用。

我看到人们像这样注册他们的标签的例子(在推送->编辑脚本中):

exports.register = function (registration, registrationContext, done) {

var userId = registrationContext.user.userId;  

   registration.tags.push(userId);

done();
}; 

但是我没有使用身份验证,所以我的 user 变量未定义。我得到的只是我的项目 table (item.id) 中的唯一标识符和注册 ID (item.regid)。 如何让我的标签正常工作?这是我的插入内容:

function insert(item, user, request) {
console.log("Registration ID -> " + item.regid);
var payload = {
    "data": {
          "message": "notification added"
         }
};  

request.execute({
success: function() {
    // If the insert succeeds, send a notification.
    push.gcm.send(item.regid, payload, {
        success: function(pushResponse) {
            console.log("Sent push:", pushResponse, payload);
            request.respond();
            },              
        error: function (pushResponse) {
            console.log("Error Sending push:", pushResponse);
            request.respond(500, { error: pushResponse });
            }
        });
    },
error: function(err) {
    console.log("request.execute error", err)
    request.respond();
}
}); 
}

通知中心目前无法发送到特定设备。

模拟这个的机制是通过注册过程和标签。直接使用通知中心 API。当您注册设备时,请使用适合该设备或用户的标签注册收听。例如,注册监听标签 USER-userid 和 DEVICE-deviceid。

然后,当你想发送到特定设备时,发送到DEVICE-deviceid,如果你想发送到注册到特定用户的所有设备,你可以发送到USER-userid;显然,用适当的值替换 userid 和 deviceid。

您可以在此处找到有关标签的更多信息:https://msdn.microsoft.com/library/azure/dn530749.aspx

如果您想向特定用户发送通知,您必须像 Adrian hall 在之前的回答中描述的那样使用唯一标签或唯一标识符注册他们,我同意 him.As Android 中的唯一标识符,您可以使用 DeviceId 并在 Ios identifierForVendor 中它们是唯一的并且永远不会改变。