为什么GCM不返回规范ID

时间:2016-05-10 12:53:54

标签: node.js google-cloud-messaging

有一个客户端应用程序通过GCM接收推送通知。

它的工作方式如下:1。应用程序在GCM中注册并获取registration_id(令牌)2。我将此令牌发送到应用程序服务器3.进一步从应用程序服务器发送推送通知我设置此令牌并对应设备收到消息。

我对以下情况感兴趣: - 用户删除应用程序(删除令牌) - 用户安装应用程序并再次接收令牌并将其发送到服务器

问题是,据我所知,在服务器上用户现在是一个新标记?

在这种情况下,我可以依赖规范ID吗?

如果是这样,为什么我得到响应值canonical_ids:0而不是2

例如:

{ multicast_id: 8398272353674231000,
  success: 3,
  failure: 2,
  canonical_ids: 0,
  results: 
   [ { message_id: '0:1462880284140861%5dee2b8a5dee2b8a' },
     { message_id: '0:1462880284140859%5dee2b8a5dee2b8a' },
     { error: 'NotRegistered' },
     { message_id: '0:1462880284140863%5dee2b8a5dee2b8a' },
     { error: 'NotRegistered' } ] }

我的GCM服务器请求代码

http = require('http');
var request = require('request');
os = require('os');

var mGcmUsers = [];

var interfaces = os.networkInterfaces()
var addresses = []
for (var k in interfaces) {
    for (var k2 in interfaces[k]) {
        var address = interfaces[k][k2];
        if (address.family == 'IPv4' && !address.internal) {
            addresses.push(address.address)
        }
    }
}


//gcm variables
var API_KEY = "MyKey";

/******************************** HTTP SERVER *************************************/

http.createServer(function (req, res) {

    if (req.method == 'POST') {
        console.log('\nHTTP POST');
        req.on('data', function (data) {
            console.log('gcm user token: ' + data)
        });
    }

    if (req.method == 'GET') {
        console.log('\nHTTP GET');
    }

}).listen(8000, addresses[0]);
console.log('HTTP server running at', addresses[0], ':8000');




var gcm = require('node-gcm');

// Create a message
// ... with default values
var message = new gcm.Message();

// ... or some given values
var message = new gcm.Message({
    collapseKey: 'data',
    priority: 'high',
    contentAvailable: true,
    delayWhileIdle: false,
    timeToLive: 10000,
    restrictedPackageName: "com.my.project",
    // dryRun: true,

    data: {
        message: 'Message from gcm server',
    },

    notification: {
        title: "Hello, World",
        icon: "ic_launcher",
        body: "This is a notification that will be displayed ASAP."
    }
});


// Set up the sender with you API key
var sender = new gcm.Sender(API_KEY);

// Add the registration tokens of the devices you want to send to
var registrationTokens = [];
registrationTokens.push('e-nzMnczgAc:APA91bH7lVRdY4Vgo3rIwzl4VJNQzmMXeCp5KogXb88WlpU-G7QLUTm41o9h8qMJdpWHXidW0Up6fnPJxypFaqsQGAj4UeuCGfFMFeFOrZLislgKU6dWyj3rmFTkPo9k4PCIxn3zAm3p');
registrationTokens.push('cxC0GdUP38k:APA91bFi_Rkf2vStLphQI-FRQxdT9QWbAOg593j2M4XKBtgesIQnZJ9w2MR_Uajj9oOjirIlYLMJQgzkrgN1hOUkA3iqrPxQ3cvu64jbluyP9Zy1Q73q4cCCribIrvcfu34sOWbyF1YP');
registrationTokens.push('eGrXkmmOv3o:APA91bHx_Xdua6_2emJSY2t48a12ZLNruJkP8cTg-DUGOfFzF7N8Rm0DE0SUUiMzqyPCuaMKWWRW_HGvgicRrm_hXXinWAXP2-NYjRqdeBWS3G7CACi64hU-64GsRw34a3-5u3wiJ86G');
registrationTokens.push('eK4vCiFIu40:APA91bHw2HVlZiHCqjh6NrJ-epx5YqJKCf-CcgtO0yXXO_czPKjvozounPZRuE9I-iFzIdQCo3mi9Apr7e9NHCRi-Sv9qzn1xTyeLswv7FJWb_ZESVGDKtQeCPVpDoGwJzGS9FTag3Xv');
registrationTokens.push('dw_zOwn2X-U:APA91bGWuSCPOMsabAvjPzrrFDx8ZoQM-LtE0YKabrDaeShZV31T7Mvh-Mtim4J6X3th8UBgAgqr4OZhYjTcpkyE3J_PdapEhvjf98geqloZL7u_7vaVfLZolQKYwWaO-prTO89pVxpi');
registrationTokens.push('cb3-U8wHXgA:APA91bGx4YTolh4ti4IP7PxzCXqWM9Q1Sb23dQXBq4U5Ftrbf4zgZC0hgay4V2SAptKN4SubJ-3tanf4iiS77uQy4VpLZdtZsu7rUqAHmZ4-p1nGwQU1eIAHc3EZPAA_sN4jSWSq3Of6');

sender.send(message, { registrationTokens: registrationTokens }, function (err, response) {
    if(err) console.error(err);
    else    console.log(response);
});

0 个答案:

没有答案