2014-11-23 51 views
0

我正在写一个OSX程序,而不是管理一个会员数据库。其中一项功能是能够将消息发送给安装了伴侣应用程序的成员。如何从OSX应用程序向iOS设备发送推送通知?我得到错误107

我最近才知道,使用解析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: "} 

有人可以看到我的”我的代码出错了吗?

回答

0

绝不会失败 - 只要您发布,您会发现自己的错误。 忘记关键字:

[request setHTTPBody:postData]; 

DOH!

相关问题