【问题标题】:node-gcm: send PushNotification for all devicenode-fcm:为所有设备发送推送通知
【发布时间】:2017-05-29 14:53:03
【问题描述】:

我正在尝试使用 node.js 构建 PushNotification 服务器, 我使用“node-fcm”但它不适用于我,所以我尝试使用“node-gcm”,但我遇到了同样的问题,我不知道如何为所有用户发送通知?我需要在字段 (to:) 中写什么??

这是我的代码:

var gcm = require('node-gcm');
var Sender_ID = '55*****';
var API_KEY = 'my server key';
var sender = new gcm.Sender(API_KEY,{'proxy':'http://username:password@my_proxyinternet.com:8080' , timeout: 5000});
var message = new gcm.Message({
    notification: {
        title: "Hello, World",
        icon: "ic_launcher",
        body: "This is a notification that will be displayed."
    }
});

var registrationTokens = [];
registrationTokens.push(['All']); 
sender.send(message, { registrationTokens: 'All' }, function (err, response) {
    if (err) console.error(err + '  ERROR');
    else console.log(response + '  ELSE');
});

结果是:

{ multicast_id: -1, success: 0, failure: 1, canonical_ids: 0, results: [ { error: 'InvalidRegistration' } ] } 错误:提供的收件人密钥 'registrationTokens' 类型不正确。错误进程以退出代码 0 结束

注意:我使用 Ionic 2,可以收到来自https://console.firebase.google.com/ 的通知。

【问题讨论】:

    标签: node.js push-notification ionic2


    【解决方案1】:

    问题解决了,实际上我没有找到向所有用户发送通知的解决方案,所以我在 android 应用程序中使用了这样的主题: 在我的 ionic 应用程序中,我将主题选项添加到 android 选项中,例如:

    const options: PushOptions = {
      android: {
        topics:['A123'],
        senderID: "55*********5"
      }
    

    对于服务器,我使用了这个repositery

    最后我将这段代码写入 index.js 文件:

    var gcm = require('./lib/node-gcm');
    
    var message = new gcm.Message();
    message.addNotification({
        title: 'Alert!!!',
        body: 'Abnormal data access',
        icon: 'drawable-hdpi-icon',
        image: 'drawable-hdpi-icon',
        alert: 'true',
        sound: 'true'
    });
    //Add your mobile device registration tokens here
    RETRY_COUNT = 4;
    var regTokens = 'AAAAgXm-v**:***************************************************EaH';
    
    var sender = new gcm.Sender(regTokens,{'proxy':'http://Username:Password@my_proxy.com:8080' , timeout: 5000});
    
    sender.send(message, { topic: "/topics/A123" }, RETRY_COUNT, function (err, response) {    
        if(err) {
            console.error(err);
        } else {
            console.log(response);
        }
    });

    这就是全部步骤,希望对你有帮助

    【讨论】:

      【解决方案2】:

      如果你想使用 FCM-PUSH;这是真的例子:

      var FCM = require('fcm-push')
      
      var SERVER_API='AAA*****************************jEaH';//put your api key here
      var fcm = new FCM(SERVER_API)
      var message = { 
          to: "/topics/A123",
          //collapse_key: '55',
          priority: 'high',
          content_available: true,
          notification: {
              title: 'Title of your push notification',
              body: 'Body of your push notification'
          },
          data: {  //you can send only notification or only data(or include both)
              my_key: 'my value',
              my_another_key: 'my another value'
          }
      }
      
      fcm.send(message, function(err, response){
          if (err) {
              console.log("Something has gone wrong!")
          } else {
              console.log("Successfully sent with response: ", response)
          }
      })

      【讨论】:

        猜你喜欢
        • 2021-01-07
        • 2017-04-27
        • 1970-01-01
        • 1970-01-01
        • 2019-07-21
        • 1970-01-01
        • 2018-10-29
        • 2020-09-17
        • 1970-01-01
        相关资源
        最近更新 更多