iOS Question Firebase Sending tool needs update to support Firebase Cloud Messaging V1

mcqueccu

Well-Known Member
Licensed User
Longtime User
I integrated firebase push in my application, and one thing I noticed is the LEGACY Cloud messaging has been disabled, and it's recommended to use the Firebase Cloud Messaging API V1.
The new FCM API v1 token is shorter than the older web API key (which is now disabled by default)

screenshot-console.firebase.google.com-2022.06.07-02_11_02.png


When using the new FCM token (format AIza............), I get this error

B4X:
Waiting for debugger to connect...
Program started.
ResponseError. Reason: , Response: <HTML>
<HEAD>
<TITLE>INVALID_KEY_TYPE</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>INVALID_KEY_TYPE</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
[jobname=fcm, success=false, username=
, password=, errormessage=<HTML>
<HEAD>
<TITLE>INVALID_KEY_TYPE</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>INVALID_KEY_TYPE</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
, target=class b4j.example.main
, taskid=1, req=anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpRequest@3911c2a7, response=anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpResponse@4ac3c60d
, tag=java.lang.Object@4facf68f, invalidurl=https://invalid-url/, defaultscheme=https
, main=null, httputils2service=null]

But when I went to Cloud Messaging API and enabled the Old Cloud Messaging API which is longer and starts AAAA.................., the messaging works.
 
Solution
Google really make thing more complicated.


Endpoint:
Before
POST https://fcm.googleapis.com/fcm/send
After
POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send

Authorization:
Before
Authorization: key=AIzaSyZ-1u...0GBYzPu7Udno5aA
After
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA

Payload:
Before
JSON:
{
  "to": "/topics/news",
  "notification": {
    "title": "Breaking News",
    "body": "New news story available."
  },
  "data": {
    "story_id": "story_12345"
  }
}
After
JSON:
{
  "message": {
    "topic": "news",
    "notification": {
      "title": "Breaking News"...

aeric

Expert
Licensed User
Longtime User
You mean the sending tool attached in the first post?

As far as I know, it has been working all the while.
 
Upvote 0

mcqueccu

Well-Known Member
Licensed User
Longtime User
You mean the sending tool attached in the first post?

As far as I know, it has been working all the while.
Thank you @aeric for the response.

Yes in my submission, the tool works with my old projects using the long web API token (AAAA*************)

But the screenshot I posted above shows they are recommending FCM v1 API which when I use with the sending tool, I get the error I posted, and I think something changed in the FCM v1.

Which of the tokens do you use?
Shorter one
AIza******

Or longer one
AAAA*****
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Thank you @aeric for the response.

Yes in my submission, the tool works with my old projects using the long web API token (AAAA*************)

But the screenshot I posted above shows they are recommending FCM v1 API which when I use with the sending tool, I get the error I posted, and I think something changed in the FCM v1.

Which of the tokens do you use?
Shorter one
AIza******

Or longer one
AAAA*****
Sorry, I have been long time not working with FCM. I just checked one of my old apps. It is indeed using the AAAA... version but I remember I was using the one with Alza...
Let me check again and double confirm.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Google really make thing more complicated.


Endpoint:
Before
POST https://fcm.googleapis.com/fcm/send
After
POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send

Authorization:
Before
Authorization: key=AIzaSyZ-1u...0GBYzPu7Udno5aA
After
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA

Payload:
Before
JSON:
{
  "to": "/topics/news",
  "notification": {
    "title": "Breaking News",
    "body": "New news story available."
  },
  "data": {
    "story_id": "story_12345"
  }
}
After
JSON:
{
  "message": {
    "topic": "news",
    "notification": {
      "title": "Breaking News",
      "body": "New news story available."
    },
    "data": {
      "story_id": "story_12345"
    }
  }
}

To get the ya29.xxxxxxx, we need to use a valid OAuth 2.0 access token. So the question is how I can get this token?

So I modify the project from here:

Here is the tested Sending Tool 2
 

Attachments

  • SendingTool2.zip
    4.9 KB · Views: 651
Last edited:
Upvote 1
Solution

aeric

Expert
Licensed User
Longtime User
Take note:
Replace fire-push-24359 with your project ID. I forgot to remove my project id.
B4X:
Job.PostString("https://fcm.googleapis.com/v1/projects/fire-push-24359/messages:send", jg.ToString)

I only do for sending FCM to B4i app using topic.
Maybe Erel or anyone can improve my code above for sending FCM to B4A and B4i.
 
Upvote 0
Top