本文介绍了解析服务器:iOS 推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发送推送通知,我已在 parse-server/certs 文件夹中设置了我的 .p12 文件.这是我在 index.js 中的代码:

I'm trying to make a push notification, I had set my .p12 files in parse-server/certs folder. Here is my code in my index.js:

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:

I would like to send push notification by cloud code. So here is my 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 项目中调用它:

And I call it in my Xcode project:

[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];
    }
}];

但这不起作用:[Error]: {"code":1,"message":"Internal server error."} (Code: 1, Version: 1.12.0)

在 Parse.com 上,我的数据库中有安装"字段:

On Parse.com, I've "Installation" field in my DB:

你有什么想法吗?

推荐答案

我的云代码不正确,这里是好的代码:

My cloud code wasn't right, here is the good code:

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});
});

这篇关于解析服务器:iOS 推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 04:22