Parse-Server 推送通知

Posted

技术标签:

【中文标题】Parse-Server 推送通知【英文标题】:Parse-Server push notifications with 【发布时间】:2018-04-04 17:20:57 【问题描述】:

我一直在尝试更新使用 parse.com 的旧应用程序。我已经设置了自己的解析服务器实例并指示应用程序使用新实例。除了推送通知外,应用程序现在一切正常。我创建了一个新的 firebase 应用程序并启用了 FCM。我已将密钥放在 myConfig.json 中,当我从终端开始解析时会引用它。它看起来像:


    "appId": "idofapp",
    "masterKey": "master,
    "cloud": "/home/ubuntu/cloud/",
    "databaseURI": "mongodb://localhost:27017/test-app",
    "push": 
        "android":
            "senderId": "99999999999",
            "apiKey": "BigLongKeyGeneratedFromFirebase"
            
    

我创建了一个我在配置文件中引用的 cloud/main.js。它看起来像:

Parse.Cloud.define("SendPush", function(request) 
     var msg = request.params.message;
     var query = new Parse.Query(Parse.User);
     var user = request.params.user;
     query.equalTo("objectId", user);
     Parse.Push.send(
     where: query,
     data:
          alert: 
               "title" : msg,
               "body" : msg
          ,
          sound: 'default'
     
     , 
     useMasterKey: true,
     success: function()
          response.success("Push Sent!");
     ,
     error: function(error)
          response.error("Error while trying to send push " + error.message);
     
     );
);

我启用了详细日志记录,因为它没有给我任何关于为什么没有发送推送通知的信息。它打印出以下五次,仅更改 Status-Id。

    verbose: REQUEST for [POST] /parse/functions/SendPush: 
  "message": "You have a new message ",
  "user": "bXYVlwltiN"
 method=POST, url=/parse/functions/SendPush, x-parse-session-token=r:6d11549cc2f2e7ffd64ba07a6d327e8d, x-parse-application-id=app, x-parse-client-version=a1.15.8, x-parse-app-build-version=1, x-parse-app-display-version=1.0, x-parse-os-version=8.0.0, user-agent=Parse Android SDK 1.15.8 (com.company.name/1) API Level 26, x-parse-installation-id=44123cf9-65fa-4871-8798-22eafa291711, content-type=application/json, content-length=57, host=ec2-19-198-39-191.us-west-2.compute.amazonaws.com:1337, connection=Keep-Alive, accept-encoding=gzip, message=You have a new message , user=bXYVlwltiN
verbose: REQUEST for [POST] /parse/push: 
  "where": 
    "objectId": "bXYVlwltiN"
  ,
  "data": 
    "alert": 
      "title": "You have a new message ",
      "body": "You have a new message "
    ,
    "sound": "default"
  
 method=POST, url=/parse/push, user-agent=node-XMLHttpRequest, Parse/js1.11.1 (NodeJS 7.10.1), accept=*/*, content-type=text/plain, host=localhost:1337, content-length=269, connection=close, objectId=bXYVlwltiN, title=You have a new message , body=You have a new message , sound=default
verbose: RESPONSE from [POST] /parse/push: 
  "headers": 
    "X-Parse-Push-Status-Id": "ZOrTd4OylW"
  ,
  "response": 
    "result": true
  
 X-Parse-Push-Status-Id=ZOrTd4OylW, result=true

未发送任何通知。你能告诉我我错过了什么吗?

我已将云功能更改为以下,但仍然出现相同的错误:

Parse.Cloud.define("SendPush", function(request, response) 

var userId = request.params.user;
    var message = "sening a test message"; //request.params.message;
    var queryUser = new Parse.Query(Parse.User);
    queryUser.equalTo('objectId', userId);
    var query = new Parse.Query(Parse.Installation);
    query.matchesQuery('user', queryUser);

    Parse.Push.send(
      where: query,
      data: 
        alert: message,
        badge: 0,
        sound: 'default'
      
    , 
      success: function() 
        console.log('##### PUSH OK');
        response.success();
      ,
      error: function(error) 
        console.log('##### PUSH ERROR');
        response.error('ERROR');
      ,
      useMasterKey: true
    );

);

【问题讨论】:

【参考方案1】:

据我所知,响应是未定义的。它表明它是您的 sendPush 云函数的第二个参数

【讨论】:

我已将云函数更改为上面引用的函数,但仍然收到相同的消息。 您仍然收到相同的消息是什么意思?此外,您可能应该尝试基于 Promise 的 API。它更加强大和可靠。 Parse.Push.send( where, data , useMasterKey: true )).then(() => response.success(); ).catch(() => response.error('ERRROR' ); ); 我刚刚将代码切换为使用 firebase 来消除解析服务器。这样做花了更少的时间,然后我才弄清楚为什么通过解析无法推送...

以上是关于Parse-Server 推送通知的主要内容,如果未能解决你的问题,请参考以下文章

如何自定义推送通知?

托管解析是不是支持推送通知?

在 Heroku 上使用推送通知

解析服务器无法发送推送通知?

推送通知不起作用 / Heroku-mLab

未收到在 Back4App 上解析的 Android 推送通知