如何从OSX应用程序向IOS设备发送推送通知?我收到错误107

时间:2014-11-23 04:07:01

标签: ios macos parse-platform

我正在编写OSX程序,而不是管理会员数据库。其中一项功能是能够向已安装随播应用程序的成员发送消息。

我最近了解到使用解析SDK的推送通知只能在IOS设备上完成,而且要从Mac上完成,我必须使用restful api。没问题。但我遇到了问题,。

我有方法:

-(void)sendPushNotification:(NSString*)strTheDeviceToken TheMessage:(NSString*) strTheMessage{
    //POST
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"https://api.parse.com/1/push"]];
    [request setHTTPMethod:@"POST"];
    NSString *post = [NSString stringWithFormat:@"{\"where\":{"];
    post = [post stringByAppendingString:[NSString stringWithFormat:@"\"deviceToken\":\""]];
    post = [post stringByAppendingString:strTheDeviceToken];
    post = [post stringByAppendingString:[NSString stringWithFormat:@"\"},\"data\":{"]];
    post = [post stringByAppendingString:[NSString stringWithFormat:@"\"alert\": \""]];
    post = [post stringByAppendingString:strTheMessage];
    post = [post stringByAppendingString:[NSString stringWithFormat:@"\"}}"]];
    NSLog(@"The JSON post = '%@'",post);
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    //[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"MyAPIdHere" forHTTPHeaderField:@"X-Parse-Application-Id"];
    [request setValue:@"MyRestKeyHere" forHTTPHeaderField:@"X-Parse-REST-API-Key"];

    NSURLResponse *requestResponse;
    NSData *requestHandler = [NSURLConnection sendSynchronousRequest:request returningResponse:&requestResponse error:nil];

    NSString *requestReply = [[NSString alloc] initWithBytes:[requestHandler bytes] length:[requestHandler length] encoding:NSASCIIStringEncoding];
    NSLog(@"requestReply: %@", requestReply);
    NSLog(@"DONE!");
}

发送的JSON看起来正确形成:

{
    "where": {
        "deviceToken": "-tokenhere-"
    },
    "data": {
        "alert": "This is a test"
    }
}

但结果是:

{"code":107,"error":"invalid json: "}

有人可以在我的代码中看到我出错的地方吗?

1 个答案:

答案 0 :(得分:0)

永远不会失败 - 只要您发布 - 您就会发现自己的错误。 忘了关键线:

[request setHTTPBody:postData];

DOH!

相关问题