Unable to retrieve the FCM server key for the recipient's app. (expo-server-sdk)
See original GitHub issue🐛 Bug Report
Hello, Im using expo-server-sdk on my nodejs server to send notifications to my ejected expo app, so i follow expo-notifications instructions, i just upload my server api key succesfully by typing: expo push:android:upload --api-key <your-token-here>
but getting following error: “Unable to retrieve the FCM server key for the recipient’s app. Make sure you have provided a server key as directed by the Expo FCM documentation.”
Summary of Issue (just a few sentences)
[
{
id: 'deb0636b-2f71-4c2f-8424-244bd532b9b5',
status: 'error',
message: "Unable to retrieve the FCM server key for the recipient's app. Make sure you have provided a server key as directed by the Expo FCM documentation.",
details: { error: 'InvalidCredentials', fault: 'developer' }
}
]
Environment - output of expo diagnostics
& the platform(s) you’re targeting
Reproducible Demo
this is my nodejs function to send push notification:
const { Expo } = require('expo-server-sdk')
const sendNotifications = (somePushTokens, message) => {
console.log("sendNotifications / somePushTokens: ", somePushTokens);
return new Promise(async(resolve, reject) => {
// Create a new Expo SDK client
let expo = new Expo();
// Create the messages that you want to send to clients
let messages = [];
for (let pushToken of somePushTokens) {
// Each push token looks like ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]
// Check that all your push tokens appear to be valid Expo push tokens
if (!Expo.isExpoPushToken(pushToken)) {
console.error(`Push token ${pushToken} is not a valid Expo push token`);
continue;
}
// Construct a message (see https://docs.expo.io/versions/latest/guides/push-notifications)
const messageHandled = {
to: pushToken,
sound: 'default',
body: 'This is a test notification',
data: { withSome: 'data' },
}
messages.push(messageHandled)
}
// The Expo push notification service accepts batches of notifications so
// that you don't need to send 1000 requests to send 1000 notifications. We
// recommend you batch your notifications to reduce the number of requests
// and to compress them (notifications with similar content will get
// compressed).
let chunks = expo.chunkPushNotifications(messages);
let tickets = [];
(async () => {
// Send the chunks to the Expo push notification service. There are
// different strategies you could use. A simple one is to send one chunk at a
// time, which nicely spreads the load out over time:
for (let chunk of chunks) {
try {
let ticketChunk = await expo.sendPushNotificationsAsync(chunk);
console.log(ticketChunk);
tickets.push(...ticketChunk);
// NOTE: If a ticket contains an error code in ticket.details.error, you
// must handle it appropriately. The error codes are listed in the Expo
// documentation:
// https://docs.expo.io/versions/latest/guides/push-notifications#response-format
} catch (error) {
reject(error);
console.error(error);
}
}
})();
// Later, after the Expo push notification service has delivered the
// notifications to Apple or Google (usually quickly, but allow the the service
// up to 30 minutes when under load), a "receipt" for each notification is
// created. The receipts will be available for at least a day; stale receipts
// are deleted.
//
// The ID of each receipt is sent back in the response "ticket" for each
// notification. In summary, sending a notification produces a ticket, which
// contains a receipt ID you later use to get the receipt.
//
// The receipts may contain error codes to which you must respond. In
// particular, Apple or Google may block apps that continue to send
// notifications to devices that have blocked notifications or have uninstalled
// your app. Expo does not control this policy and sends back the feedback from
// Apple and Google so you can handle it appropriately.
let receiptIds = [];
for (let ticket of tickets) {
// NOTE: Not all tickets have IDs; for example, tickets for notifications
// that could not be enqueued will have error information and no receipt ID.
if (ticket.id) {
receiptIds.push(ticket.id);
}
}
let receiptIdChunks = expo.chunkPushNotificationReceiptIds(receiptIds);
(async () => {
// Like sending notifications, there are different strategies you could use
// to retrieve batches of receipts from the Expo service.
for (let chunk of receiptIdChunks) {
try {
let receipts = await expo.getPushNotificationReceiptsAsync(chunk);
console.log(receipts);
// The receipts specify whether Apple or Google successfully received the
// notification and information about an error, if one occurred.
for (let receiptId in receipts) {
let { status, message, details } = receipts[receiptId];
if (status === 'ok') {
continue;
} else if (status === 'error') {
console.error(
`There was an error sending a notification: ${message}`
);
if (details && details.error) {
// The error codes are listed in the Expo documentation:
// https://docs.expo.io/versions/latest/guides/push-notifications/#individual-errors
// You must handle the errors appropriately.
console.error(`The error code is ${details.error}`);
}
}
}
resolve()
} catch (error) {
resolve()
console.error(error);
}
}
})();
})
}
module.exports = sendNotifications;
this is similar issue: https://forums.expo.io/t/failed-to-authenticate-with-the-fcm-server-ensure-the-fcm-server-key-you-uploaded-is-correct/25399
@cruzach please help!
- This should include as little code as possible, please don’t simply link your entire project
- Sharing a link to a Snack is a GREAT way to provide a reproducible demo 😃
- If a reproducible demo, or a complete list of steps from blank project to bug, are not provided, it is very likely your issue will be closed
- If you need more guidance, please see https://stackoverflow.com/help/mcve
As an added benefit- creating a repro may help you identify the source of the bug, which means we are one step closer to fixing it! Thanks for helping us help you!
Steps to Reproduce
Expected Behavior vs Actual Behavior
Issue Analytics
- State:
- Created 3 years ago
- Comments:15 (7 by maintainers)
Got the same problem. It`s happened when i build/publish new app. To solve this:
expo push:android:show
(it was unconfigured for me)expo push:android:upload --api-key <your-token-here>
, replacing <your-token-here> with the string you just copied. (how to get token - read https://docs.expo.io/push-notifications/using-fcm/#uploading-server-credentials ) I believe it will help somebody =)facing same error:
application get expo push token, but when we are trying to send something with https://expo.io/notifications - get error
InvalidCredentials: Unable to retrieve the FCM server key for the recipient's app. Make sure you have provided a server key as directed by the Expo FCM documentation.
UPDATE: strange, but there there is FCM server key in expo servers was cleared. Repushing key via
expo push:android:upload --api-key <your-token-here>
solve problem. But why this key can clear himself?