使用Strongloop实现推送通知

时间:2014-08-21 09:41:48

标签: push-notification strongloop

我正在尝试使用strongloop loopback sdk 2.0.我尝试使用以下代码https://github.com/strongloop/loopback-component-push/tree/master/example/server loopback version 1.7.0.但是当我尝试使用version 2.0编译时,它会抛出我的错误

  

错误:model-config.json中的数据采用不受支持的1.x格式。

我还尝试过强循环教程,但它仍然无效。任何人都有关于如何使用loopback 2.0实现PUSH通知的建议或示例代码?

2 个答案:

答案 0 :(得分:5)

  1. 创建4个模型applicationinstallationnotificationpush
  2.   

    common/models/application.json   

    {
      "name": "push",
      "plural": "Push",
      "base": "Model",
      "properties": {},
      "validations": [],
      "relations": {},
      "acls": [],
      "methods": []
    }
    
      

    common/models/installation.json

    {
      "name": "installation",
      "base": "Installation",
      "properties": {},
      "validations": [],
      "relations": {},
      "acls": [],
      "methods": []
    }
    
      

    common/models/notification.js

    {
      "name": "notification",
      "base": "Notification",
      "properties": {},
      "validations": [],
      "relations": {},
      "acls": [
        {
          "principalType": "ROLE",
          "principalId": "$everyone",
          "permission": "ALLOW",
          "property": "sendMessage"
        }
    
      ],
      "methods": []
    }
    
      

    common/models/push.json

    {
      "name": "push",
      "plural": "Push",
      "base": "Model",
      "properties": {},
      "validations": [],
      "relations": {},
      "acls": [],
      "methods": []
    }
    
      

    server/datasource.json

    ...
    ...
    "push": {
        "name": "push",
        "connector": "loopback-component-push",
        "installation": "installation",
        "notification": "notification",
        "application": "application"
    }
    
      

    common/models/notification.js

      module.exports = function(Notification) {
    
       var badge = 1;
    
        //DEFINING A PROPERTY IN NOTIFICATION FOR SENDING  PUSH MESSAGE
        Notification.sendMessage = function(message, registrationId, from, callback) {
              var app = this.app;
            from = from? from:'COMPANY_NAME';
            sendMessage(app, message, registrationId, from, callback);
        }//sendMessage Notification method..
    
    
    
        //FUNCTION FOR SENDING PUSH MESSAGE..
        var sendMessage = function(app, message, registrationId, from, callback){
          var Application = app.models.application;
          var PushModel = app.models.push;
    
          var note = new Notification({
            expirationInterval: 3600, // Expires 1 hour from now.
            badge: badge++,
            //  sound: 'ping.aiff',
            message: message,
            messageFrom: from
          });
    
          PushModel.notifyById(registrationId, note, function (err) {
            if (err) {
              console.error('Cannot notify %j: %s', registrationId, err.stack);
              return callback(err);
            }
            console.log('Pushing notification to %j', registrationId);
            callback(null, []);
          });
        }//sendMessage function
    
    
    
    
      //Now registering the method
        Notification.remoteMethod(
            'sendMessage', 
            {
              accepts: [{arg: 'message', type: 'string', required:true},
              {arg: 'registrationId', type: 'string', required:true},
              {arg: 'from', type: 'string', required:true}],
              description: "Sending message from notification.."
            }
        )
    
    }//module.exports
    
      

    现在在server/文件夹中创建一个文件push-service.js

        module.exports = function(app) {
        var Notification = app.models.notification;
        var Application = app.models.application;
        var PushModel = app.models.push;
        function startPushServer() {
    
        PushModel.on('error', function(err) {
          console.error('Push Notification error: ', err.stack);
        });
    
        // Pre-register an application that is ready to be used for testing.
        // You should tweak config options in ./config.js
    
        var loopbackApp = {
          id: 'loopback-push-application',
          userId: 'strongloop',
          name: config.appName,
    
          description: 'loopback Push Notification Service',
          pushSettings: {
            apns: {
              certData: "APPLE CERT. DATA",
              keyData: "APPLE KEY DATA",
              pushOptions: {
                // Extra options can go here for APN
              },
              feedbackOptions: {
                batchFeedback: true,
                interval: 300
              }
            },
            gcm: {
              serverApiKey: "PASTE YOUR GOOGLE GCM KEY HERE"
            }
          }
        };
    
    
    
        updateOrCreateApp(function(err, appModel) {
          if (err) {
            throw err;
          }
          console.log('Application id: %j', appModel.id);
        });
    
    
        function updateOrCreateApp(cb) {
          Application.findOne({
              where: {
                id: loopbackApp.id
              }
            },
            function(err, result) {
              if (err) cb(err);
              if (result) {
                delete loopbackApp.id;
                result.updateAttributes(loopbackApp, cb);
              } else {
                return registerApp(cb);
              }
            });
        } //updateOrCreate function
    
    
    
        Application.beforeSave = function(next) {
          if (this.name === loopbackApp.name) {
            this.id = 'loopback-push-application';
          }
          next();
        };
    
        Application.register(
          loopbackApp.userId,
          loopbackApp.name, {
            description: loopbackApp.description,
            pushSettings: loopbackApp.pushSettings
          },
          function(err, app) {
            if (err) {
              return cb(err);
            }
            return cb(null, app);
          }
        );
        } //register App
    
        } //startPushServer
    
        startPushServer();
    
        };
    
      

    现在终于在server.js

    ....
    ....
    
    //Adding push service to the backend..
    require('./push-service')(app);
    
    ....
    

    现在运行环回服务器打开api explorer并转到NOTIFICATION-> SendMessage方法并输入任何消息,它将在已连接的设备上发送推送通知。

    注意:您还需要从android / iphone配置推送服务以启用发送推送应用程序。有关详细信息,请查看环回文档。

答案 1 :(得分:0)