ios – Is there a means of getting a everlasting entry token from Google service account?


I am presently engaged on an iOS app (utilizing Swift) that sends push notifications between two customers when any of them sends a message to the opposite (and when different occasions occur as properly). For that, I am utilizing Firebase Cloud Messaging and that is the swift perform that really sends the notification:

        let accessToken  = "xxxx"
        
        let urlString = "https://fcm.googleapis.com/v1/initiatives/( Ok.FCMessaging.projectId)/messages:ship"
        
        let url = NSURL(string: urlString)!
        
        let paramString: [String : Any] = ["message": [
            "apns" : ["payload" : ["aps": ["alert": ["title": title, "body": body], "sound": "default"] as [String : Any], OrderModel.CodingKeys.orderId.rawValue: order.orderId!, OrderModel.CodingKeys.senderId.rawValue: order.senderId] as [String : Any]],
            "token" : receiverToken] as [String : Any]
        ]
        let request = NSMutableURLRequest(url: url as URL)
        request.httpMethod = "POST"
        request.httpBody = attempt? JSONSerialization.knowledge(withJSONObject:paramString)
        request.setValue("software/json", forHTTPHeaderField: "Content material-Kind")
        request.setValue("Bearer (accessToken)", forHTTPHeaderField: "Authorization")
        let job =  URLSession.shared.dataTask(with: request as URLRequest)
        job.resume()

The entry token is obtained by operating this python code domestically on the Mac:

import argparse
import json
import requests
import google.auth.transport.requests
from google.oauth2 import service_account


PROJECT_ID = 'xxxx'
BASE_URL = 'https://fcm.googleapis.com'
FCM_ENDPOINT = 'v1/initiatives/' + PROJECT_ID + '/messages:ship'
FCM_URL = BASE_URL + "https://stackoverflow.com/" + FCM_ENDPOINT
SCOPES = ['https://www.googleapis.com/auth/firebase.messaging']

def access_token():
    credentials = service_account.Credentials.from_service_account_file('/Customers/...-firebase-xxxx.json', scopes=SCOPES)
    request = google.auth.transport.requests.Request()
    credentials.refresh(request)
    return credentials.token

Now every little thing works wonderful (i.e. the notifications get despatched and acquired) after I copy the entry token into the swift perform, however it expires after one hour and I’ve to run the code once more and replica the entry token once more.
Since I haven’t got a server to mechanically run the python perform on, it looks like I am lacking one thing.
Are you able to please inform me the way to get a everlasting entry token for the app or the proper means of sending push notifications between two units in iOS?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles