2
在ios中,我使用的是Firebase數據庫和FCM。我必須通過設備令牌從設備發送推送通知給設備。我有FCM API「https://fcm.googleapis.com/fcm/send」這是POST方法,並添加參數 像:Firebase從設備發送推送通知(IOS)
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
...
},
}
,但是當我嘗試調用這個API這常是返回我一個錯誤,而同樣API從Android設備調用,這是在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? – user3589771
我不知道郵遞員,我必須嘗試從FCM控件發送推送,設備從設備令牌推送 –
內容類型必需'acceptableContentTypes = application/json'。你在頂部提到。你的錯誤顯示你只是設置內容類型'「Content-Type」=「text/html; charset = UTF-8」;' – user3589771