繁体   English   中英

解析服务器:iOS推送通知

[英]Parse-server: iOS Push notification

我正在尝试发出推送通知,我已将我的.p12文件设置在parse-server / certs文件夹中。 这是我的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:

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

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

在Parse.com上,我在数据库中具有“安装”字段: 屏幕截图

你有什么主意吗?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM