18

I'm trying to send notifications to a specific device with Firebase but am getting the following error:

{"r": {"results":[{"error":{"code":"messaging/mismatched-credential","message":"The credential used to authenticate this SDK does not have permission to send messages to the device corresponding to the provided registration token. Make sure the credential and registration token both belong to the same Firebase project."}}],"canonicalRegistrationTokenCount":0,"failureCount":1,"successCount":0,"multicastId":5165415028752234000}}

I'm not sure what is causing this, I'm using the service account associated with the project and the proper device id.

Any help would be great, thanks!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Joe Scotto
  • 10,936
  • 14
  • 66
  • 136

5 Answers5

14

This error message can be found in the documentation for FCM.

It means that you've initialized the Admin SDK with a credential for a project that is not the same as the device registration token.

To put it another way: for security reasons, you can't simply send messages to any device token that you come up with. You can only send messages to device tokens via apps in the same project.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • But the app I'm sending to is in the same project? – Joe Scotto Feb 09 '18 at 20:22
  • I have no way of telling. You have to initialize with a credential that comes from the same project. That's something only you have access to. – Doug Stevenson Feb 09 '18 at 20:39
  • 1
    I'm using the service account from the exact same project. Make no sense to me why it wouldn't be working. – Joe Scotto Feb 09 '18 at 20:41
  • All I can say is that you should double check all your data and make sure your service account is still valid. After you have double checked, if there is still a problem, contact Firebase support. https://firebase.google.com/support/contact/troubleshooting/ – Doug Stevenson Feb 09 '18 at 20:44
  • Did you find a solution? i have the same issue – Gillis Haasnoot May 16 '18 at 19:09
  • like the answer says, I was trying to send push notifications to the wrong FCMToken (the 152 char token that my client app gets from Firebase after sending a deviceId to firebase) – ehacinom Nov 01 '18 at 18:02
  • If you change your json credential files you need to do a clean on the project or android studio will not build the new credentials. – Elemental May 21 '21 at 12:05
13

The issue was with my google-services.json. My app uses two projects and was defaulting to the first project. Adding the second project json file ended up fixing this since it can now send the tokens to the proper project.

Joe Scotto
  • 10,936
  • 14
  • 66
  • 136
  • Hi Joe, I am facing the same problem. But I didn't get your point. Can you please elaborate in detail? – Jay Sojitra Apr 15 '19 at 10:54
  • 3
    I faced the same problem, it is with Android studio, it is caching the JSON file across builds if not clean build the project at first place – Suraj Malviya Jan 24 '20 at 04:59
  • The amount of times I have used the development google-services.json file by mistake! – MadMac May 05 '21 at 23:14
10

I faced the same problem. It took me two days to figure out that I need to enable the Firebase Cloud Messaging API through google cloud platform. Without enabling it, it also yields the error "mismatched-credential". I really wish that the error message could be more specific.

Martin Z
  • 171
  • 1
  • 6
  • Thank you ! Very annoy for enable ! – BSQ Apr 16 '18 at 07:41
  • 4
    @Martin Z How do I achieve this? – Mark Adel Nov 04 '18 at 01:51
  • 2
    1) From the Firebase Console for your project go to settings service accounts. 2) Click the Service accounts for Google cloud platform link to go to the related google cloud project 3) From the left hamburger menu got to APIs and Services 4) Find "Firebase Cloud Messaging" (don't confuse it with "Cloud Messaging") 5) Enable API – Chris.D Aug 10 '19 at 20:21
  • Enabling is not the requirement here. My project is working without enablilng the api from GCP as well. – Suraj Malviya Jan 24 '20 at 05:00
1
This worked for me try it:

1- Go to google cloud.

2- then go to API and Services.

3- then Enabled API and Services.

4- then turn on the cloud messaging and Turn on Firebase cloud messaging API.

0

Enable Firebase Cloud Messaging API in Google Cloud Platform.

Note that I wasn't able to find it in the section APIs & Services Dashboard but I had to browse the API Library

After you enable it you can find it in:
Google Cloud Platform > Navigation Menu > APIs & Services > Dashboard > Firebase Cloud Messaging API

Direct Link: https://console.cloud.google.com/apis/api/fcm.googleapis.com/overview

Credits to niocncn.

NewestStackOverflowUser
  • 2,792
  • 5
  • 22
  • 31