Push notification 使用Strongloop实现推送通知

Push notification 使用Strongloop实现推送通知,push-notification,strongloop,Push Notification,Strongloop,我尝试使用strongloop loopback sdk 2.0。我尝试使用以下代码,即loopback version 1.7.0。但当我尝试使用version 2.0编译时,它抛出了错误 错误:model-config.json中的数据采用不受支持的1.x格式 我也尝试过按照强循环教程,但仍然不起作用。任何人都有关于如何使用环回2.0实现推送通知的建议或示例代码?请查看此示例-请查看此示例- 创建4个模型应用程序,安装,通知,推送 在common/models/application.jso

我尝试使用
strongloop loopback sdk 2.0。
我尝试使用以下代码,即
loopback version 1.7.0。
但当我尝试使用
version 2.0
编译时,它抛出了错误

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


我也尝试过按照强循环教程,但仍然不起作用。任何人都有关于如何使用环回2.0实现推送通知的建议或示例代码?

请查看此示例-

请查看此示例-

  • 创建4个模型
    应用程序
    安装
    通知
    推送
  • common/models/application.json中
    

    common/models/installation.json中

    common/models/notification.js

    common/models/push.json中

    server/datasource.json

    common/models/notification.js

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

    现在终于进入server.js

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

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

  • 创建4个模型
    应用程序
    安装
    通知
    推送
  • common/models/application.json中
    

    common/models/installation.json中

    common/models/notification.js

    common/models/push.json中

    server/datasource.json

    common/models/notification.js

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

    现在终于进入server.js

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


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

    我们将很快使用适用于2.0的文档/github进行更新。我们将很快使用适用于2.0的文档/github进行更新。
    {
      "name": "push",
      "plural": "Push",
      "base": "Model",
      "properties": {},
      "validations": [],
      "relations": {},
      "acls": [],
      "methods": []
    }
    
    {
      "name": "installation",
      "base": "Installation",
      "properties": {},
      "validations": [],
      "relations": {},
      "acls": [],
      "methods": []
    }
    
    {
      "name": "notification",
      "base": "Notification",
      "properties": {},
      "validations": [],
      "relations": {},
      "acls": [
        {
          "principalType": "ROLE",
          "principalId": "$everyone",
          "permission": "ALLOW",
          "property": "sendMessage"
        }
    
      ],
      "methods": []
    }
    
    {
      "name": "push",
      "plural": "Push",
      "base": "Model",
      "properties": {},
      "validations": [],
      "relations": {},
      "acls": [],
      "methods": []
    }
    
    ...
    ...
    "push": {
        "name": "push",
        "connector": "loopback-component-push",
        "installation": "installation",
        "notification": "notification",
        "application": "application"
    }
    
      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
    
        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();
    
        };
    
    ....
    ....
    
    //Adding push service to the backend..
    require('./push-service')(app);
    
    ....