1

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

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

我也按照强循环教程尝试过,但它仍然不起作用。有人对如何使用 PUSH 通知有建议或示例代码loopback 2.0吗?

4

2 回答 2

5
  1. 创建 4 个模型application, installation, notification,push

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 配置推送服务以启用发送推送应用程序。有关详细信息,请查看环回文档。

于 2015-11-20T09:52:57.897 回答
0

请查看此示例 - https://github.com/strongloop/loopback-component-push/tree/master/example/server-2.0

于 2014-09-02T21:04:30.810 回答