【发布时间】:2016-11-29 23:59:48
【问题描述】:
我正在尝试学习使用 azure 移动应用,但在使用 NotificationHub 时遇到了严重问题。我有 Azure 的 Imagine 订阅。我创建了一个带有 azure 后端的 android 移动应用。我在 azure 门户上创建了一个与 azure 移动应用关联的通知中心。 为了在通知中心注册应用程序,我使用了本教程中的代码:
用户在 azure 后端通过身份验证之前使用他们的 google 帐户、microsoft 帐户或 facebook 帐户。通过为表脚本 Users.js 编写的以下节点 js 代码将新用户插入到表 Users 中。我想要一个推送通知来欢迎新用户。
var azureMobileApps = require('azure-mobile-apps');
var logger = require('azure-mobile-apps/src/logger');
var table = azureMobileApps.table();
table.access = 'authenticated';
/**
* Adds the email address from the claims to the context item - used for
* insert operations
* @param {Context} context the operation context
* @returns {Promise} context execution Promise
*/
function addEmailToContext(context) {
/*
* Getting claim fields
*/
return context.user.getIdentity().then((data) => {
if( data.microsoftaccount != undefined){
context.item.email = data.microsoftaccount.claims.emailaddress;
context.item.name = data.microsoftaccount.claims.givenname;
context.item.surname = data.microsoftaccount.claims.surname;
}
if( data.google != undefined){
context.item.email = data.google.claims.emailaddress;
context.item.name = data.google.claims.givenname;
context.item.surname = data.google.claims.surname;
context.item.picture_url = data.google.claims.picture;
}
if( data.facebook != undefined){
context.item.email = data.facebook.claims.emailaddress;
context.item.name = data.facebook.claims.givenname;
context.item.surname = data.facebook.claims.surname;
}
logger.info('[tables/Users.js] --> NEW USER REGISTERED:'
+'\n\t Name:'+context.item.name
+'\n\t Surname:'+context.item.surname
+'\n\t Email:'+context.item.email);
// Execute the insert. The insert returns the results as a Promise,
// Do the push as a post-execute action within the promise flow.
return context.execute()
.then(function (results) {
// Only do the push if configured
if (context.push) {
// Mobile Apps adds a user tag when registering for push notifications
// Define the GCM payload.
var payload = {
"data": {
"message": 'Welcome '+context.item.username
}
};
context.push.gcm.send(context.user.id, payload, function (error) {
if (error) {
logger.error('Error while sending push notification: ', error);
} else {
logger.info('Push notification sent successfully!');
}
});
}
// Don't forget to return the results from the context.execute()
return results;
})
.catch(function (error) {
logger.error('Error while running context.execute: ', error);
});
});
}
// CREATE - add or overwrite the authenticated user
table.insert(addEmailToContext);
module.exports = table;
根据How to use the Azure Mobile Apps Node.js SDK教程中的“如何:使用标签向经过身份验证的用户发送推送通知”
“当认证用户注册推送通知时,用户ID标签会自动添加到注册中。” 因此,在 Users.js 中,按照本教程的建议,我编写了以下代码来向用户发送推送通知。
context.push.gcm.send(context.user.id, payload, function (error) {
if (error) {
logger.error('Error while sending push notification: ', error);
} else {
logger.info('Push notification sent successfully!');
}
});
使用此代码可以成功发送推送通知,但设备没有收到任何通知。如果我使用 null 而不是 context.user.id 那么所有设备都会正确接收推送通知:
context.push.gcm.send(null, payload, function (error) {
if (error) {
logger.error('Error while sending push notification: ', error);
} else {
logger.info('Push notification sent successfully!');
}
});
当用户注册到集线器时,我还尝试调用以下自定义 API 来创建标签。调用的API如下:
var logger = require('azure-mobile-apps/src/logger');
exports.post = function(req, res) {
logger.info('[api/registerTag.js] --> Invoked');
// Get the notification hub used by the mobile app.
var push = req.azureMobile.push,
installationId = req.get('X-ZUMO-INSTALLATION-ID'),
tags = req.body.tag.toString();
// Define an update tags operation.
var updateOperation = [{
"op": "add",
"path": "/tags",
"value": tags
}];
// Update the installation to add the new tags.
push.patchInstallation(installationId, updateOperation, function(error) {
if(error){
logger.error('[api/registerTag.js] --> An error occurred while adding'
+'the following tags: \n\t'+tags, error);
res.status(error.statusCode).send(error.detail);
} else {
logger.info('[api/registerTag.js] --> The following tags have been added'
+'to the Notification Hub: \n\t'+tags, error);
res.status(200).send(tags);
}
});
};
在控制台上打印标签已成功添加。但是,如果我然后像这样修改 Users.js 代码:
...
// Only do the push if configured
if (context.push) {
// Mobile Apps adds a user tag when registering for push notifications
var userTag = '_UserId:' + context.user.id;
logger.info("TAG "+userTag);
// Define the GCM payload.
var payload = {
"data": {
"message": 'Welcome '+context.item.username
}
};
context.push.gcm.send(userTag, payload, function (error) {
if (error) {
logger.error('Error while sending push notification: ', error);
} else {
logger.info('Push notification sent successfully!');
}
});
}
...
再次没有收到任何东西。我还尝试了将标签列入白名单或使用移动应用程序的“推送”部分自动添加它们,如图所示:
图片链接:i.stack.imgur.com/KBvQI.png
但问题仍然存在。希望可以有人帮帮我。谢谢。
【问题讨论】:
标签: android node.js azure azure-mobile-services azure-notificationhub