FCM unable receive push notification after app ugrade - ios

I am using FCM for my app. In first build I have successfully added firebase. Then I have upgraded app to new version and it doesn't receive any push notification.
Even firebase server call retruns proper device data, i.e. whether the device is registered or not. Here is the code:
func getUserTopicSubscribeWithToken() {
print("getUserTopicSubscribeWithToken")
var token = ""
if Messaging.messaging().fcmToken != nil {
token = Messaging.messaging().fcmToken!
}
let urlString = "https://iid.googleapis.com/iid/info/\(token)?details=true"
let url = URL(string: urlString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!)
var request = URLRequest(url: url!)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("key=\(serverKey)", forHTTPHeaderField: "Authorization")
let session = URLSession.shared
session.dataTask(with: request) {data, response, err in
print("Entered the completionHandler")
if (data != nil && err == nil) {
if let topics = String.init(data: data!, encoding: .utf8) {
print("topics :: \(topics)")
let cleanResult = topics.replacingOccurrences(of: "\\", with: "")
if let dict = JSONResultHelper.convertToDictionary(text: cleanResult) {
print("dict :: \(dict)")
if let allTopics = dict["rel"] as? [String:Any] {
print("allTopics :: \(allTopics)")
if let values = allTopics["topics"] as? [String:Any] {
print("values :: \(values)")
for key in values.keys {
AppDelegate.sharedInstance.moduleManager().apiModule?.subscribeToChannel(channelName: key)
}
}
}
}
}
}
}.resume()
}
This call gives me fcm details as expected.
I have also tried to send notification to single device using fcm token, but It didn't work.
Please Note:
It is working perfectly fine on fresh install but not on upgraded one.

Related

Swift : HttpRequest works on Simulator but not on Device (iPhone 11)

I have an Authentication Manager that send SignIn and SignUp request. Code works good on simulator and fetch data from database but not working on device. On device It gives http 500 error. I check the textfields before sending the request and they seem fine.
Any suggestions to solve this issue ?
func performSignIn(email: String, password: String){
//1. Create URL
if let url = URL(string: Constants.signInURL){
var request = URLRequest(url: url)
//Request Body
var body = [String : Any]()
body["user"] = ["email": email,
"password": password]
do {
request.httpBody = try JSONSerialization.data(withJSONObject: body, options: [])
} catch {
print("JSON serialization failed: \(error)")
}
// Change the URLRequest to a POST request
request.httpMethod = "POST"
//Need to tell that request has json in body.
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
//2. Create URLSession
let session = URLSession(configuration: .default)
//3. Give Session a task
let task = session.dataTask(with: request) { (data, response, error) in
if error != nil{
DispatchQueue.main.async {
delegate?.failedWithError(error: error!)
return
}
}
if let safeData = data{
if let signInResponse = parseJSON(safeData){
//Who has the delegate run signInSuccess method
delegate?.signInSuccess(signInResponse)
}
}
}
//4. Start Task
task.resume()
}
}

Spotify API Authorization Error (Swift)

I've made a little App in Swift where a user can search the Spotify database for songs. I am using the Web API Console > Search for an Item. My problem is the new OAuth system where you have to sign-in and all that stuff. My authorization is ok, but when I'm trying to get an access token with the following code, it's returning me the following error: {"error":"server_error","error_description":"Unexpected status: 400"}. My code is:
let keys = "<MY_APPLICATION_KEYS>"
let url = NSURL(string: "https://accounts.spotify.com/api/token")
let session = URLSession.shared
let request = NSMutableURLRequest(url: url! as URL)
request.httpMethod = "POST"
request.setValue("Basic \(keys)", forHTTPHeaderField: "Authorization")
request.setValue("client_credentials", forHTTPHeaderField: "grant_type")
let task = session.dataTask(with: request as URLRequest) { (data, response, error) in
guard let _: Data = data, let _: URLResponse = response, error == nil else {
print(error!)
return
}
let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("Data: \(dataString!)")
self.parseData(JSONData: data!)
}
task.resume()
}
var accessToken = ""
func parseData(JSONData : Data) {
do {
var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONStandard
if let token = readableJSON["access_token"] as? String {
accessToken = token
}
print("Access Token: \(accessToken)")
updateTokenInFirebase()
}
catch{
print(error)
}
Any help would be very appreciated, thank you very much in advance!
Documentation of the Web API: Web API Link
I am using on the Client Credentials Flow the first method.
I know it's been ~1 year since you posted this but I had the same issue and after a few tries was able to get it. You can test this in Playground.
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
if let url = URL(string: "https://accounts.spotify.com/api/token") {
var postRequest = URLRequest(url: url)
postRequest.httpMethod = "POST"
let bodyParams = "grant_type=client_credentials"
postRequest.httpBody = bodyParams.data(using: String.Encoding.ascii, allowLossyConversion: true)
let id = "your client id"
let secret = "your secret"
let combined = "\(id):\(secret)"
let combo = "\(id):\(secret)".toBase64()
postRequest.addValue("Basic \(combo)", forHTTPHeaderField: "Authorization")
let task = URLSession.shared.dataTask(with: postRequest) { (data, response, error) in
guard let data = data else {
return
}
print(String(data: data, encoding: String.Encoding.utf8)!)
}
task.resume()
}
extension String {
func fromBase64() -> String? {
guard let data = Data(base64Encoded: self) else {
return nil
}
return String(data: data, encoding: .utf8)
}
func toBase64() -> String {
return Data(self.utf8).base64EncodedString()
}
}
I know this is really late, but the issue is with this line:
request.setValue("client_credentials", forHTTPHeaderField: "grant_type")
According to the authorization guide, this should be in the body of the request, not the headers.

send token to server Swift 3

i'm trying to send a token to my server to check if token is still valid or has expired. the token value is pulled from saved data on the device. But no matter what i try it keeps giving me "missing token" which is the status message for when i've sent empty string.
// load content in user var
user = UserDefaults.standard.value(forKey: "parseJSON") as? NSDictionary
// if user is once logged in / register, keep him logged in
if user != nil {
print(user as Any)
let userToken = user!["token"] as? String
let url = NSURL(string: "http://fmm.dummy.com/api/v1/auth/me")!
var request = URLRequest(url: url as URL)
request.setValue("Bearer \(String(describing: userToken))", forHTTPHeaderField: "Authorization")
request.httpMethod = "POST"
When i edited and did this, the error message change to "invalid token" which meant i was sending something. it just wasn't correct
// load content in user var
user = UserDefaults.standard.value(forKey: "parseJSON") as? NSDictionary
// if user is once logged in / register, keep him logged in
if user != nil {
print(user as Any)
let userToken = user!["token"] as? String
let a: Character = "<"
let b: Character = ">"
let url = NSURL(string: "http://fmm.dummy.com/api/v1/auth/me")!
var request = URLRequest(url: url as URL)
request.setValue("Bearer .append\(a)\(String(describing: userToken)).append\(b)", forHTTPHeaderField: "Authorization")
request.httpMethod = "POST"
//launch session
URLSession.shared.dataTask(with: request) { data, response, error in
can someone please help me figure out what am doing wrong?
thanks
///EDIT
Hi when i edited as you had suggested and came up with this, i couldn't even print to console. Here is the full code for your consideration. Thanks for the response.
if let user = UserDefaults.standard.object(forKey: "parseJSON") as? [String:Any],
let userToken = user["token"] as? String {
let url = URL(string: "http://fmm.dummy.com/api/v1/auth/me")!
var request = URLRequest(url: url)
let tokenString = "Bearer " + userToken
request.setValue(tokenString, forHTTPHeaderField: "Authorization")
request.httpMethod = "POST"
//launch session
URLSession.shared.dataTask(with: request) { data, response, error in
//check if no error
if error == nil{
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
guard let parseJSON = json else{
print ("Error While Parsing")
return
}
print(parseJSON)
let statusMsg = parseJSON["status"] as? String
if statusMsg == "OK"{
//token still valid
self.login()
}else {
//token is invalid
// get main queue to communicate back to user
DispatchQueue.main.async(execute: {
let message = parseJSON["status_msg"] as! String
appDelegate.infoView(message: message, color: colorSmoothRed)
})
}
}catch{
// get main queue to communicate back to user
DispatchQueue.main.async(execute: {
let message = "\(error)"
appDelegate.infoView(message: message, color: colorSmoothRed)
})
return
}
}else{
// get main queue to communicate back to user
DispatchQueue.main.async(execute: {
let message = error!.localizedDescription
appDelegate.infoView(message: message, color: colorSmoothRed)
})
return
}
}.resume()
}
I'm trying to write code in appdelegate.swift that checks if the token saved from a previous login is still valid or has expired! the saved object on the user device is as follows. When i print user to the console i get this. ::--
Optional({
response = {
token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjIsImlzcyI6Imh0dHA6XC9cL3htbS54bW9iaWxlbW9uZXkuY29tXC9hcGlcL3YxXC9hdXRoIiwiaWF0IjoxNDk2ODUyNTkxLCJleHAiOjE0OTY4NTYxOTEsIm5iZiI6MTQ5Njg1MjU5MSwianRpIjoiOTFhYjE0MjZkMGZmN2RjZDVjOTQzNTlkZGY4OGY0ZmYifQ.WaQFprfRbJMDQ72IORtWrcSmUqY3EaJp5BAqjijvUAc";
verified = 1;
};
status = OK;
})
You are using too many optionals which can cause unexpected literal "Optional(<value>)" when calling the description method.
Unwrap all optionals safely with optional bindings:
if let user = UserDefaults.standard.object(forKey: "parseJSON") as? [String:Any],
let userToken = user["token"] as? String {
let url = URL(string: "http://fmm.dummy.com/api/v1/auth/me")!
var request = URLRequest(url: url)
let tokenString = "Bearer " + userToken
request.setValue(tokenString, forHTTPHeaderField: "Authorization")
request.httpMethod = "POST"
...
As always, never use valueForKey to get a single object from UserDefaults, use always objectForKey, and never use NSDictionary for a property list compliant dictionary, use always [String:Any].

Where do I specify ReloadIgnoringLocalCacheData for NSURLSession in Swift 2

I have an app that makes an API call every 5 seconds using NSURLSession and p2-oauth2. I'm running into an issue of it returning cached data instead of the updated information from the API. I read this post by Matt Thompson where he describes the different cache policies, the one I think I need to use is ReloadIgnoringLocalCacheData. I think it's suppose to be put in the AppDelegate DidFinishLaunchingWithOptions functions. But, the problem I'm having is I don't know where or how to specify it. I haven't found any Swift solutions. Can anyone tell me what my function should say?
If it's helpful, here is my API request:
let urlPath = "https://sandbox-api.uber.com/v1/requests/\(uberRequestId)"
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
guard let endpoint = NSURL(string: urlPath) else { print("Error creating endpoint");return }
let request = appDelegate.oauth.request(forURL: NSURL(string:urlPath)!)
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.HTTPMethod = "GET"
//get response from Uber and iterate through to find Uber Product ID.
NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
do {
guard let dat = data else { throw JSONError.NoData }
let result = try NSJSONSerialization.JSONObjectWithData(dat, options: NSJSONReadingOptions.MutableContainers)
print(result)
//set status
status = result["status"] as! String
print("found status...returning it back -> \(status)")
completion(status: "\(status)")
} catch let error as JSONError {
print(error.rawValue)
print("ERROR NEEDS TO BE HANDLED.")
} catch {
print(error)
print("ERROR NEEDS TO BE HANDLED.")
}
}.resume()
Here is the final request that properly sets the cache policy. I added one line with ReloadIgnoringLocalCacheData.
let urlPath = "https://sandbox-api.uber.com/v1/requests/\(uberRequestId)"
let url:NSURL = NSURL(string: urlPath)!
let session = NSURLSession.sharedSession()
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let request = appDelegate.oauth.request(forURL: NSURL(string:urlPath)!)
request.HTTPMethod = "GET"
//added this line to set cache policy
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
let task = session.dataTaskWithRequest(request) {
(
let data, let response, let error) in
guard let _:NSData = data, let _:NSURLResponse = response where error == nil else {
print("error")
return
}
let dataString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print(dataString)
}
task.resume()
Assuming the OAuth API returns a mutable request, you can set its cachePolicy property to NSURLRequestCachePolicy.ReloadIgnoringCacheData.

Uber Invalid OAuth 2.0 credentials provided Uber Authentication In ios Swift

I'm implementing the Uber's Request Endpoint in my iOS (Swift) App. The Request API/Endpoint requires the user authentication with the app, here is the doc.
For this I'm using this Oauth2.0 library
What I did is
successfully integrated the Library in my project (xCode) with the help of given installation instructions.
In My AppDelegate
let uber_OAuth_Settings = [
"client_id": "XXXXXXX9vtKzobvXXXXXX",
"client_secret": "EXXXXXXXXyFUNCa_Wez6AXXXXXXXnrXtxus",
"authorize_uri": "https://login.uber.com/oauth/authorize",
"token_uri": "https://login.uber.com/oauth/token",
"redirect_uris": ["jamesappv2://oauth/callback"], // don't forget to register this scheme
] as OAuth2JSON
var oauth:OAuth2CodeGrant!
in my method didFinishLaunchingWithOptions of Appdelegate
oauth = OAuth2CodeGrant(settings: uber_OAuth_Settings)
oauth.viewTitle = "Uber Login Service" // optional
oauth.verbose = true // For Logs
Don't forget to register url scheme i.e ("redirect_uris": ["jamesappv2://oauth/callback"])
goto your app's Target -> info Tab -> Url Types -> Click (+), image attached
In AppDelegate add method given Below and Handle the Callback Url
func application(application: UIApplication,
openURL url: NSURL,
sourceApplication: String?,
annotation: AnyObject?) -> Bool {
// you should probably first check if this is your URL being opened
var splitUrl = url.absoluteString!.componentsSeparatedByString(":")
if splitUrl[0] == ("jamesappv2") {
oauth.handleRedirectURL(url)
}
return true
}
Now in my viewController I did like this on myBtnClick
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let url = appDelegate.oauth.authorizeURL()
UIApplication.sharedApplication().openURL(url)
appDelegate.oauth.onAuthorize = { parameters in
println("Did authorize with parameters: \(parameters)")
self.navigationController?.pushViewController(self.PersonalDriverUber_VC, animated: true)
//On Authorization Goto another ViewController using pushViewController of navigationcontroller Method
}
appDelegate.oauth.onFailure = { error in // `error` is nil on cancel
if nil != error {
println("Authorization went wrong: \(error!.localizedDescription)")
}
}
Here is my debug log, I'm getting the valid response:
OAuth2: Handling redirect URL jamesappv2://oauth/callback?state=4B0EB812&code=0sXXXXXXTX7yEbS1XXXXXHuw
OAuth2: Successfully validated redirect URL
OAuth2: Authorizing against https://login.uber.com/oauth/token?state=38158941&grant_type=authorization_code&code=0sXXXXXXXX1jxTrdFQT9Huw&client_secret=EIXXXXXXXNCa_Wez6XXXXXw0BlnrXtxus&client_id=fXXXXXXXy2LOUo9vtKXXXXXQ1nUDO&redirect_uri=jamesappv2%3A%2F%2Foauth%2Fcallback
OAuth2: Exchanging code 0swNXXXXX7yXXXXXXdFQT9Huw with redirect jamesappv2://oauth/callback for token at Optional("https://login.uber.com/oauth/token")
OAuth2: Did receive access token: Dfq3XXXXXXuWgpaqFXXXXXXXgXW, refresh token: EmStT7FEXHRMlS8odPzs1nsha0ObjK
Did authorize with parameters: [token_type: Bearer, expires_in: 2592000, access_token: XXXXXXOZuWgXXXXXXXXuJYOmgXW, refresh_token: EXXXXXHRMlS8oXXXXXXXa0ObjK, scope: profile, last_authenticated: 1430121470]
Notice I'm getting the valid access_token
Here I'm stuck
As per DOCs says in STEP4 *USE BEARER TOKEN
Pass the access_token returned in the response in the Authorization header with the type Bearer to make requests on behalf of a user.*
curl -H 'Authorization: Bearer YOUR_ACCESS_TOKEN' 'https://api.uber.com/v1/products?latitude=37.7759792&longitude=-122.41823'
I am not getting the point. How should I pass the access_token in Header with type Bearer? I have done like below
func callRequestAPI(url:String){
let request = appDelegate.oauth.request(forURL: NSURL(string:url)!)
request.HTTPMethod = "POST"
let postString = "product_id="+selectedUberProductId+"&start_latitude="+start_lat+"&start_longitude="+start_lng+"&end_latitude="+end_lat+"&end_longitude="+end_lng
println(postString)
let tempData: NSData = appDelegate.oauth.accessToken.dataUsingEncoding(NSUTF8StringEncoding)!
let base64LoginString = tempData.base64EncodedStringWithOptions(nil)
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.setValue("Bearer \(base64LoginString)", forHTTPHeaderField: "Authorization")
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request) { data, response, error in
if error != nil {
println("error=\(error)")
return
}
println("response = \(response)")
let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
println("responseString = \(responseString)")
}
task.resume()
}
but I'm getting following response
response = <NSHTTPURLResponse: 0x1a284b50> { URL: https://sandbox-api.uber.com/v1/requests } { status code: 401, headers {
"Content-Length" = 75;
"Content-Type" = "application/json";
Date = "Mon, 27 Apr 2015 10:22:01 GMT";
Server = nginx;
"Strict-Transport-Security" = "max-age=31536000; includeSubDomains; preload";
"x-uber-app" = "uberex-sandbox";
"x-xss-protection" = "1; mode=block";
} }
responseString = Optional({"message":"Invalid OAuth 2.0 credentials provided.","code":"unauthorized"})
Finally I have done it :)
I changed the method like below and it Worked
func callRequestAPI(url:String){
var configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
var session = NSURLSession(configuration: configuration)
let params:[String: AnyObject] = [
"product_id" : selectedUberProductId,
"start_latitude" : start_lat,
"start_longitude" : start_lng,
"end_latitude" : end_lat,
"end_longitude" : end_lng]
let request = appDelegate.oauth.request(forURL: NSURL(string:url)!)
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.HTTPMethod = "POST"
var err: NSError?
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions.allZeros, error: &err)
let task = session.dataTaskWithRequest(request) {
data, response, error in
if let httpResponse = response as? NSHTTPURLResponse {
if httpResponse.statusCode != 202 {
println("response was not 202: \(response)")
return
}
}
if (error != nil) {
println("error submitting request: \(error)")
return
}
// handle the data of the successful response here
var result = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.allZeros, error: nil) as! NSDictionary
println(result)
if let request_id: String = result["request_id"] as? String{
println(request_id)
}
if let driver: String = result["driver"] as? String{
println(driver)
}
if let eta: Int = result["eta"] as? Int{
println(eta)
}
if let location: String = result["location"] as? String{
println(location)
}
if let status: String = result["status"] as? String{
println(status)
}
if let surge_multiplier: Int = result["surge_multiplier"] as? Int{
println(surge_multiplier)
}
if let vehicle: String = result["vehicle"] as? String{
println(vehicle)
}
}
task.resume()
}
here is the Response I Got, Parsing is also given in my above method
{
driver = "<null>";
eta = 15;
location = "<null>";
"request_id" = "ea39493d-b718-429f-8710-00a34dcdaa93";
status = processing;
"surge_multiplier" = 1;
vehicle = "<null>";
}
Enjoy
Updated for Swift 2. I used the same setup and library for oauth that Qadir describes in his question. I updated his request to work in Swift 2. Hope this helps others.
uberRequest:
let params:[String:AnyObject] = [
"product_id" : uberProduct,
"start_latitude" : userLat,
"start_longitude" : userLng,
"end_latitude" : barLat,
"end_longitude" : barLng]
let urlPath = "https://sandbox-api.uber.com/v1/requests"
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
var session = NSURLSession(configuration: configuration)
guard let endpoint = NSURL(string: urlPath) else { print("Error creating endpoint");return }
let request = appDelegate.oauth.request(forURL: NSURL(string:urlPath)!)
request.setValue("application/json; charset=utf-8", forHTTPHeaderField:"Content-Type")
request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions.PrettyPrinted)
request.HTTPMethod = "POST"
print("Prepare to make request -> \(request)")
let task = NSURLSession.sharedSession().dataTaskWithRequest(request){ data, response, error in
if error != nil{
print("Error -> \(error)")
return
}
do {
let result = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)
print("Result -> \(result)")
} catch {
print("Error -> \(error)")
}
}
task.resume()
It returns:
Result -> Optional(["driver": <null>, "request_id": 5834384c-7283-4fe6-88a7-e74150c6ab30, "surge_multiplier": 1, "location": <null>, "vehicle": <null>, "status": processing, "eta": <null>])
To use the token just follow step 5 of the instructions in the OAuth2 library, like you did before you started to try to sign it yourself a second time. The request has already been signed and has the Bearer token set up, there is nothing left to do for you:
let url = NSURL(string: "https://api.uber.com/v1/products?latitude=37.7759792&longitude=-122.41823")
let req = appDelegate.oauth.request(forURL: url)
// customize your request, if needed. E.g. for POST:
req.HTTPMethod = "POST"
// send the request
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(req) { data, response, error in
if nil != error {
// something went wrong
}
else {
// check the response and the data
// you have just received data with an OAuth2-signed request!
}
}
task.resume()

Resources