I can’t push notifications with my api express js to my device (android & ios)
I read all the documentation about https://firebase.google.com/docs/cloud-messaging/migrate-v1 and some others documentation talking about it.
I followed these instructions to get simple notifications: https://firebase.google.com/codelabs/firebase-fcm-flutter#0
I created a service account for my app and fcm by following theses instructions:
https://help.emarsys.com/hc/en-us/articles/18224997987602-Android-integration-Creating-a-Google-service-account-to-enable-push-notifications
Before the migrations, my notifications were working.
I tried to update it but it bug.
API
- version: v20.11.1
packages:
- “firebase-admin”: “^11.0.0”,
- “google-auth-library”: “^8.9.0”,
Flutter
- version: 3.22.3
packages:
- firebase_core: ^2.32.0
- firebase_analytics: ^10.10.7
- firebase_messaging: ^14.9.4
Flutter:
class TestFCM {
static void init() async {
final messaging = FirebaseMessaging.instance;
final settings = await messaging.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
if (kDebugMode) {
print('Permission granted: ${settings.authorizationStatus}');
}
String? token = await messaging.getToken();
print('token = $token');
}
}
I print token to get it and test in my api like so:
API:
const { GoogleAuth } = require('google-auth-library');
const FLUTTER_NOTIFICATION_CLICK = 'FLUTTER_NOTIFICATION_CLICK';
//Here i put my serviceAccountPath
const serviceAccountPath = '';
const auth = new GoogleAuth({
keyFilename: serviceAccountPath,
scopes: ['https://www.googleapis.com/auth/firebase.messaging'],
});
const getAccessToken = async () => {
const client = await auth.getClient();
const token = await client.getAccessToken();
return token.token;
};
const admin = require('firebase-admin');
admin.initializeApp({
credential: admin.credential.applicationDefault(),
databaseURL: process.env.databaseURL,
});
const sendMessage = async (message) => {
const accessToken = await getAccessToken();
try {
const response = await fetch(
'https://fcm.googleapis.com/v1/projects/odopassdb-test/messages:send',
{
method: 'POST',
headers: {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ message }),
},
);
const responseData = await response.json();
console.log('response data', responseData);
console.log('response details', JSON.stringify(responseData.error.details));
return responseData;
} catch (error) {
console.error('error notification', error);
}
};
// Here i put my fcmToken getted from the app
const fcmToken = '';
const message = {
data: {
title: 'title',
body: 'body',
click_action: FLUTTER_NOTIFICATION_CLICK,
screen: '',
},
token: fcmToken,
};
sendMessage(message);
In commented section i enter my value.
I generated the serviceAccount via firebase. I verified senderId, it is correct.
However it failed and return this error:
response data {
error: {
code: 403,
message: 'SenderId mismatch',
status: 'PERMISSION_DENIED',
details: [ [Object] ]
}
}
response details [{"@type":"type.googleapis.com/google.firebase.fcm.v1.FcmError","errorCode":"SENDER_ID_MISMATCH"}]
The API V1 on firebase is enabled:
Firebase Cloud Messaging API (V1)Enabled
Also the legacy is enabled but it looks like i cannot disabled this, and if it use it, it say deprecrated endpoint (when i was using old package of firebase messaging) so it shouldn’t be the error.
How can i resolve it please ?
Have a nice day!
- Use API V1 to test
- Recreate an account with https://help.emarsys.com/hc/en-us/articles/18224997987602-Android-integration-Creating-a-Google-service-account-to-enable-push-notifications but it didn’t changed the serviceAccount key.