推送宝 Node.js SDK
安装
Node.js版本要求:>=0.6
npm install tuisongbao
Example 1: 初始化客户端
var client = new require('tuisongbao').Client('tuisongbao-api-key', 'tuisongbao-api-secret');
Example 2: 推送消息给所有用户
client.sendNotificationToAll('app-key', 'hello world', function (err, nid) {
if (err) return console.log(err);
console.log('notification id:' + nid);
});
Example 3: 推送消息给订阅了特定频道的用户
client.sendNotificationByChannels('app-key', ['channel1'], 'hello world', function (err, nid) {
if (err) return console.log(err);
console.log('notification id:' + nid);
});
Example 4: 推送消息给特定版本的用户
client.sendNotificationByAppVersion('app-key', ['version1'], 'hello world', function (err, nid) {
if (err) return console.log(err);
console.log('notification id:' + nid);
});
Example 5: 推送消息给特定版本中订阅了指定频道的用户
client.sendNotificationByChannelsAndAppVersion('app-key', ['channel1'], ['version1'], 'hello world', function (err, nid) {
if (err) return console.log(err);
console.log('notification id:' + nid);
});
Example 6: 推送消息给特定用户
client.sendNotificationByTokens('app-key', ['token1'], 'hello world', function (err, nid) {
if (err) return console.log(err);
console.log('notification id:' + nid);
});
说明:
- 所有 sendNotification 方法均有一个可选参数 options(object) ,可用于设置 extra 、 est 等可选项
- 当推送给苹果设备时,如果 extra 中含有 aps 字段, message 会被忽略
- est 必须为 Date 对象(不需要考虑时区),例如:
new Date(2013, 7, 7, 0, 0, 0)
Example 7: 推送消息给全部苹果用户, 且定时在2013年7月7日 00:00:00发送
var options = {};
options.extra = {
"aps" : {
"alert" : {
"body" : "hello world",
"action-loc-key" : "",
"loc-key" : "",
"loc-args" : [],
"launch-image" : ""
},
"badge" : 1,
"sound" : "alert.aov"
},
"key1" : "value1",
"key2" : "value2"
};
options.est = new Date(2013, 7, 7, 0, 0, 0);
client.sendNotificationToAll('app-key', 'this wil be ignored', options, function (err, nid) {
if (err) return console.log(err);
console.log('notification id:' + nid);
});
Example 8: 查询推送状态
client.queryNotificationStatus('app-key', nid, function (err, status) {
if (err) return console.log(err);
console.log('success count: ' + status.success + ' failed count: ' + status.failed);
});