Jump to content

Vladsrb

Members
  • Content Count

    2
  • Joined

  • Last visited

Everything posted by Vladsrb

  1. Currently i also get Internal server Error for GetIt, so for FB SDK you can use the link provided by Dave Nottage. For reading FB token from IOS app using FB SDK: Manual Hint: When you add Project/Deployment - GoogleService-Info.plist (downloaded from Firebase for IOS) - check 'Remote path' column, must be: .\
  2. Hi, i was facing with the same problem... This is my workable solution, in short 🙂 uses System.Net.HttpClient, JOSE.Core.Builder, JOSE.Core.JWT, JOSE.Core.JWA, JOSE.Types.Bytes, JOSE.Types.JSON, JOSE.Signing.RSA, JOSE.Core.JWK JOSE is taken from https://github.com/paolo-rossi/delphi-jose-jwt Info: FGoogleProjectID : is your project ID like 'name-95dc3e3' (console.cloud.google.com) FGoogleServiceEmail: like 'firebase-adminsdk-9tm7h@XXXXXXXX.iam.gserviceaccount.com' FGoogleServicePrivateKey: '-----BEGIN PRIVATE KEY-----...' taken from the JSON file when you create a key for a Google service account (you need to remove the \n characters from the key) function CreateGoogleTokenString: String; begin Result := ''; var JWToken: TJWT := TJWT.Create(TJWTClaims); try try JWToken.Claims.Issuer := FGoogleServiceEmail; JWToken.Claims.SetClaim('scope', 'https://www.googleapis.com/auth/firebase.messaging'); JWToken.Claims.Audience :='https://oauth2.googleapis.com/token'; JWToken.Claims.IssuedAt := Now; JWToken.Claims.Expiration := IncMinute(Now, 60); // not more than 60 var AJWKKey: TJWK := TJWK.Create(FGoogleServicePrivateKey); var LSignature: TJOSEBytes := TJOSE.SerializeCompact(AJWKKey, TJOSEAlgorithmId.RS256, JWToken); Result := LSignature.AsString; except on e: exception do raise Exception.Create('Error creating Google Token String: ' + E.Message); end; finally FreeAndNil(JWToken); end; end; function GetOAuthToken: String; var LResponse: IHTTPResponse; LFormUrlencoded: TStringList; LRequest: THTTPClient; begin Result := ''; LRequest := THTTPClient.Create; LFormUrlEncoded := TStringList.Create; try try var AToken: String := GCCreateGoogleTokenString; LFormUrlEncoded.Add('grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer'); LFormUrlEncoded.Add('assertion=' + AToken); LResponse := LRequest.Post('https://oauth2.googleapis.com/token', LFormUrlencoded); if LResponse.StatusCode = 200 then begin Result := LResponse.ContentAsString(); var JSONObj: TJSONObject := TJSONObject.ParseJSONValue(Result) as TJSONObject; try if Assigned(JSONObj) then Result := JSONObj.GetValue<string>('access_token'); finally JSONObj.Free; end; end; except on e: exception do begin Result := ''; raise Exception.Create('Error getting Google Token: ' + E.Message); end; end; finally LFormUrlencoded.Free; LRequest.Free; end; end; sending a push message with RESTClient: function CreateBodyMessageJSON(AFBToken: String): String; // https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#Notification begin var AJSONMain: TJSONObject := TJSONObject.Create; try var AJSONMessage: TJSONObject := TJSONObject.Create; AJSONMessage.AddPair('token', AFBToken); // for all var AJSONNotification: TJSONObject := TJSONObject.Create; AJSONNotification.AddPair('body', FBody); AJSONNotification.AddPair('title', FTitle); AJSONMessage.AddPair('notification', AJSONNotification); // for Android var AJSONNotification1: TJSONObject := TJSONObject.Create; AJSONNotification1.AddPair('sound', 'default'); // AJSONNotification1.AddPair('notification_count', TJSONNumber.Create(1)); // if needed var AJSONAndroid: TJSONObject := TJSONObject.Create; AJSONAndroid.AddPair('notification', AJSONNotification1); AJSONMessage.AddPair('android', AJSONAndroid); // for IOS var AJSONAps: TJSONObject := TJSONObject.Create; AJSONAps.AddPair('sound', 'default'); AJSONAps.AddPair('badge', TJSONNumber.Create(1)); // Show badge on app icon on IOS var AJSONPayload: TJSONObject := TJSONObject.Create; AJSONPayload.AddPair('aps', AJSONAps); var AJSONApns: TJSONObject := TJSONObject.Create; AJSONApns.AddPair('payload', AJSONPayload); AJSONMessage.AddPair('apns', AJSONApns); AJSONMain.AddPair('message', AJSONMessage); Result := AJSONMain.ToJSON; finally AJSONMain.Free; end; end; RESTClientPushmessage.BaseURL := 'https://fcm.googleapis.com/v1/projects/' + FGoogleProjectID + '/messages:send'; RESTRequestPusMessage.Params.ParameterByName('Authorization').Value := 'Bearer ' + GCGetOAuthToken; // pkHTTPHEADER RESTRequestPusMessage.Params.ParameterByName('Content-Type').Value := 'application/json' // pkHTTPHEADER RESTRequestPusMessage.Params.ParameterByName('body').Value := CreateBodyMessageJSON(AFireBaseToken); // pkREQUESTBODY RESTRequestPusMessage.Execute; If i miss something let me know... For reading FB token on IOS i use: Firebase SDK for IOS 6.28 (working on Delphi 12.2)
×