【发布时间】:2017-12-07 18:45:45
【问题描述】:
在 ios 中,我使用的是 Firebase 数据库和 FCM。我必须通过设备令牌从设备向设备发送推送通知。我有 FCM API "https://fcm.googleapis.com/fcm/send" 这是 post 方法并添加参数 喜欢:
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
...
},
}
但是当我尝试调用这个 API 时,总是返回一个错误,并且从 android 设备调用相同的 API 可以在 android 中工作,而不是在 ios 这是我的代码和错误,请给我解决方案
-(void)sendPush
{
NSDictionary *noti = @{@"body": @"This is a Firebase Cloud Messaging Topic Message", @"title": @"high"};
NSDictionary *data = @{@"text": @"This is a Firebase Cloud Messaging Topic Message", @"title": @"high"};
NSDictionary *params = @{@"to": @"dbpuoXnzY3o:APA91bH3AE_XCbsLpOXycNULcSh1GHzohEFdopmzkY_n8bm78E9_5fHyY3PLCwi5HtTLU_IoOwfdbDc5qCbyzc6tD9Ahg0EviCfbAmhftmMsPqfi0WC...",@"notification":noti,@"data":data};
NSLog(@"%@",params);
[NetworkManager postNotification:params getUrl:@"https://fcm.googleapis.com/fcm/send" success:^(id response) {
NSLog(@"%@",response);
} failure:^(NSError *error) {
}];
}
在 nslog 中是这样的
{
data = {
text = "This is a Firebase Cloud Messaging Topic Message";
title = high;
};
notification = {
body = "This is a Firebase Cloud Messaging Topic Message";
title = high;
};
to = "dbpuoXnzY3o:APA91bH3AE_XCbsLpOXycNULcSh1GHzohEFdopmzkY_n8bm78E9_5fHyY3PLCwi5HtTLU_IoOwfdbDc5qCbyzc6tD9Ahg0EviCfbAmhftmMsPqfi0WC...";
}
Afnetworking调用函数
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.responseSerializer = [AFJSONResponseSerializer
serializerWithReadingOptions:NSJSONReadingAllowFragments];
// [manager.requestSerializer setValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setValue:@"AIzaSyADxgFPrxx....." forHTTPHeaderField:@"key"];
// NSLog(@"Token==%@",[GeneralSetting sharedInstance].token_number);
[manager POST:URL parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[MBProgressHUD hideHUDForView:appDelegate.window animated:YES];
success(responseObject);
////NSLog(@"success!");
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failure(error);
[MBProgressHUD hideHUDForView:appDelegate.window animated:YES];
NSLog(@"error: %@", error.description);
// [self alert:@"" title:@""];
}];
错误是
error: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unauthorized (401)" UserInfo={NSUnderlyingError=0x17024a1d0 {Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x17402fa40> { URL: https://fcm.googleapis.com/fcm/send } { status code: 401, headers {
"Cache-Control" = "private, max-age=0";
"Content-Encoding" = gzip;
"Content-Length" = 259;
"Content-Type" = "text/html; charset=UTF-8";
Date = "Tue, 04 Jul 2017 10:17:43 GMT";
Expires = "Tue, 04 Jul 2017 10:17:43 GMT";
Server = GSE;
"alt-svc" = "quic=\":443\"; ma=2592000; v=\"39,38,37,36,35\"";
"x-content-type-options" = nosniff;
"x-frame-options" = SAMEORIGIN;
"x-xss-protection" = "1; mode=block";
} },
请帮助我,请给我解决方案
我已经尝试过邮递员,这是邮递员的工作
我从代码发送的相同数据这不起作用可能是发送字典的问题 我正在使用此代码
NSDictionary *noti = @{@"body": @"Hello", @"title": @"This is test message"};
NSDictionary *params = @{@"to": [GeneralSetting sharedInstance].token_number,@"notification":noti};
NSLog(@"%@",params);
与
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setValue:@"key=AIzaSy.....tBxdYAIRp2KBG0NoS8dOqgy4" forHTTPHeaderField:@"Authorization"];
响应是
{
notification = {
body = Hello;
title = "This is test message";
};
to = "dQgCDOK3sec:APA91zFbkqkgyTNrsYsEmUJwpCtMs1VAmXF9DpF_zQfBYui3UjCA9nYtS_TK549RPnlWEfe-jSvD9MMc3lTFNsGJlyIOqNm84s8GoW1LfTGG0geuWEclb";
}
Request failed: bad request (400)
【问题讨论】:
-
你和邮递员核实了吗?那是有效的 API 吗?
-
我不知道邮递员,我必须尝试从 FCM 控制发送推送,设备从设备令牌获取推送
-
需要内容类型
acceptableContentTypes = application/json。您在顶部提到的。您的错误显示您刚刚设置了内容类型"Content-Type" = "text/html; charset=UTF-8"; -
我已经更新了我的问题,我正在使用 Postman,这正在工作,邮递员正在发送推送,但是在对代码进行一些更正后,我如何从代码中执行错误请求错误。
-
您是否比较了您的 POSTMAN 内容类型和您的回复?还要检查其他人。
标签: ios firebase push-notification firebase-cloud-messaging