简体   繁体   English

npm-gcm不会向设备发送通知

[英]npm-gcm does not send notification to the device

guys, I want to send a notification to the android device using npm-gcm and here is my code: 伙计们,我想使用npm-gcm向Android设备发送通知,这是我的代码:

var sender = new gcm.Sender('myAPIkey');

var message = new gcm.Message();

message.addNotification('title', 'Backend!!!');
message.addNotification('body', 'Bitch');
var regTokens = ['deviceToken1'];
console.log(message)
regTokens.push('deviceToken2')

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

and in the console I get: 在控制台中,我得到:

{ multicast_id: 7664202372197545000,
  success: 2,
  failure: 0,
  canonical_ids: 0,
  results:
  [ { message_id: '0:1481660978053751%cc7dad02f9fd7ecd' },
    { message_id: '0:1481660978045814%cc7dad02f9fd7ecd' } ]
 }

but the devices did not receive any notification. 但设备未收到任何通知。 What is the problem? 问题是什么? Why is this happening? 为什么会这样呢? Can you give me an advice?! 能给我个建议吗?

Based on this SO post ,there might be an issue with "notification" object on node-gcm code. 基于此SO帖子 ,node-gcm代码上的“ notification”对象可能存在问题。 Try to use message.addData and check if it works. 尝试使用message.addData并检查其是否有效。 Make sure that you followed this tutorial and this thread that gives sample code on how to send simple message to your device. 确保您遵循了本教程和该线程 ,该线程提供了有关如何向设备发送简单消息的示例代码。

var sender = new gcm.Sender(googleApiKey);
var message = new gcm.Message();
message.addData('key1','testdarinodegcm');
message.delay_while_idle = 1;
var registrationIds = [];
registrationIds.push('APA91bHCzBP6wEfExCZlSXgf0wuOC6QEWJ-7MVFl3hgaW3Jv8FslsofGJ-      qgdliyS8zjwy_W7zPaKsEZx6kbeuWdoAAexawKl1Qd6GEGV_v844n1LMRaTsWeiwI9iaLGKKq_R3scY_wuRG8uG2SZ5X9q0J67Ko3gcw');
sender.send(message, registrationIds, 4, function (err, result) {
console.log(result);
});

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM