2016-04-12 13 views
0

私はプッシュ通知をしようとしていますが、私は自分の.p12ファイルをparse-server/certsフォルダに設定していました。 index.jsのコードは次のとおりです。パーズサーバ:iOSプッシュ通知

var api = new ParseServer({ 
    databaseURI: process.env.DATABASE_URI || 'mongodb://localhost:27017/dev', 
    cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js', 
    appId: 'xx', 
    masterKey: 'xx', 
    fileKey: 'xx', 
    clientKey: 'xx', 
    serverURL: 'xx', 
    push: { 
    ios: [ 
     { 
     pdx: 'certs/ParsePushDevelopmentCertificate.p12', // Dev PFX or P12 
     bundleId: 'bundleId', 
     production: false // Dev 
     } 
    ] 
    } 
}); 

私はクラウドコードでプッシュ通知を送信したいと思います。だからここに私のmain.jsです:

Parse.Cloud.define("pushToAll", function (request, response) { 
    var message = request.params.message; 
    if (message != null && message !== "") { 
     message = message.trim(); 
    } else { 
    response.error("Must provide \"message\" in JSON data"); 
    return; 
    } 

    // Can see this at https://www.parse.com/apps/{APP_NAME}/cloud_code/log 
    var logMessage = "Sending \"{0}\" to all installations".format(message); 
    console.log(logMessage); 

    var pushQuery = new Parse.Query(Parse.Installation); 
    // pushQuery.containedIn("deviceType", ["ios", "android"]); // errors if no iOS certificate 

    // Send push notification to query 
    Parse.Push.send({ 
     where: pushQuery, // Set our installation query 
     data: { 
      alert: message 
      } 
     }, { 
     success: function() { 
      // Push was successful 
      console.log("Message was sent successfully"); 
      response.success('true'); 
     }, 
     error: function (error) { 
      response.error(error); 
     } 
    , useMasterKey: true}); 
}); 

そして私は私のXcodeプロジェクトでそれを呼び出す:

[PFCloud callFunctionInBackground:@"pushToAll" withParameters:@{@"message" : @"test"} block:^(id object, NSError *error) { 
    if (!error) { 
     NSLog(@"YES"); 
    } else { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Try Again !" message:@"Check your network" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
     [alert show]; 
    } 
}]; 

しかし、それは動作しません:Parse.comオン [Error]: {"code":1,"message":"Internal server error."} (Code: 1, Version: 1.12.0)

を、私は」私のDBの "インストール"フィールド: Screenshot

ご存知ですか?

+0

あなたが解析して接続できることを確認します - サーバーを最初に。何かを送ったり、アプリを正しく接続しているかどうかを確認してください。 –

+0

@DanLはいそれはうまく動作します:オブジェクトの追加/削除、他のPFCloudメソッドは正常に動作しますが、通知プッシュのPFCloudは機能しません。 – Viny76

+0

設定で本番と開発用の両方の証明書を設定すると、サーバーが両方向で送信しようとするため、コンソールで毎回少なくとも1つのエラーが発生し、そのうちの1つが常に失敗するため、この瞬間...いくつかのログを提供するクラウドコードが実行されるとき何が起こる –

答えて

1

マイクラウドコードは右ではなかった、ここで良いのコードは次のとおりです。

Parse.Cloud.define("pushToAll", function (request, response) { 
    var message = request.params.message; 
    console.log(message); 
    if (message != null && message !== "") { 
     message = message.trim(); 
    } else { 
    response.error("Must provide \"message\" in JSON data"); 
    return; 
    } 

    // Can see this at https://www.parse.com/apps/{APP_NAME}/cloud_code/log 
    // var logMessage = "Sending to all installations".format(message); 
    // console.log(logMessage); 

    var pushQuery = new Parse.Query(Parse.Installation); 
    // pushQuery.containedIn("deviceType", ["ios", "android"]); // errors if no iOS certificate 

    // Send push notification to query 
    Parse.Push.send({ 
     where: pushQuery, // Set our installation query 
     data: { 
      "alert": message 
      } 
     }, { 
     success: function() { 
      // Push was successful 
      console.log("Message was sent successfully"); 
      response.success('true'); 
     }, 
     error: function (error) { 
      response.error(error); 
     } 
    , useMasterKey: true}); 
}); 
+0

Githubで見つけたコードを使用していただきありがとうございます;) –

+0

@ cricket_007、ありがとうございます、私はそれをchnged、私は私のケースのために改善しました – Viny76

+0

@ Viny76、canこのクラウドプッシュを送信するためのcURL構文を共有しますか?私はvarメッセージを取り出して単純な文字列を使用する以外はあなたのfuncをコピーしました。私のcURL = curl -X POST -H "X-Parse-Application-Id:myAppId" -H "Content-Type:application/json" -d {} https://__myApp__.herokuapp.com/parse/functions/pushToAll 。異なる構文も試しましたが、エラーが発生しました。最新のエラー: "XMLHttpRequestが失敗しました:\" Parse APIに接続できません\ "デフォルトのサンプルコードが{" result ":" Hi "}を返すのでクラウドコードに到達できます。 – rockhammer