'iOS Swift - How to obtain OAuth 2.0 access token in order to send Firebase Cloud Message?

In my iOS app, I'm trying to send a push notification from one device to another using Firebase Cloud Messaging.

I am trying to use the Firebase-recommended v1 HTTP protocol (as opposed to the FCM legacy HTTP API). The v1 HTTP protocol requires sending a POST request to the url https://fcm.googleapis.com/v1/{parent=projects/*}/messages:send as described in the Firebase docs here. However, at the bottom it states that some authorization scopes are required:

Screenshot from https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages/send

It later explains here that I need to add the OAuth 2.0 access token (with the above scopes authorized) to the header (e.g. "Authorization": "Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA").

But where/how can I generate this access token? I believe the answer lies here, but I'm struggling to understand since I'm not that familiar with backend. Is there a way to obtain the token just from the client side using only Swift, or can it only be obtained from a server environment...?

Any insight is appreciated, thank you.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source