-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix bug 🐛 push notifications not working for node #282
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some of the formatting looks off. Can you please run prettier on this PR
return res.json({ ok: true, messageId: response }); | ||
} catch (e) { | ||
error(e); | ||
return res.json({ ok: false, error: 'Failed to send the message' }, 500); | ||
return res.json({ ok: false, error: `failed due to ${e}` }, 500); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We prefer not to expose the error message to the user, can you revert this?
}, | ||
token: req.deviceToken, | ||
// extra options payload here |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this comment
throwIfMissing(process.env, [ | ||
'FCM_PROJECT_ID', | ||
'FCM_PRIVATE_KEY', | ||
'FCM_CLIENT_EMAIL', | ||
'FCM_DATABASE_URL', | ||
]); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move the throwIfMissing
this to the top of the function
try { | ||
throwIfMissing(req.body, ['deviceToken', 'message']); | ||
throwIfMissing(req.body.message, ['title', 'body']); | ||
} catch (err) { | ||
return res.json({ ok: false, error: err.message }, 400); | ||
} | ||
|
||
log(`Sending message to device: ${req.body.deviceToken}`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert this
admin.initializeApp({ | ||
credential: admin.credential.cert({ | ||
projectId: process.env.FCM_PROJECT_ID, | ||
clientEmail: process.env.FCM_CLIENT_EMAIL, | ||
privateKey: process.env.FCM_PRIVATE_KEY, | ||
}), | ||
databaseURL: process.env.FCM_DATABASE_URL, | ||
});} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to try catch this
databaseURL: process.env.FCM_DATABASE_URL, | ||
}); | ||
return await admin.messaging().send(payload); | ||
console. log(`Message: ${JSON.stringify(payload)}`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this comment
Done with all the requested changes. Can you please review my code? @loks0n. |
throwIfMissing(process.env, [ | ||
'FCM_PROJECT_ID', | ||
'FCM_PRIVATE_KEY', | ||
'FCM_CLIENT_EMAIL', | ||
'FCM_DATABASE_URL', | ||
]); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move the throwIfMissing
this to the top of the function
moved |
What does this PR do?
This PR fix the issue where push notification does not work correctly for
push-notification-with-fcm
template for node.Test Plan
When trying to use push notification fcm cloud function from templates, I found that the function
is not working correctly.
Then, I came across this bug 🐛.
On previous code:
main.js - line : 26
token: req.deviceToken,
this will be
token: req.body.deviceToken,
for that reason the correct device token was not reaching to the
sendPushNotification()
.on utils.js
everytime we were calling sendPushNotification function we were initializing
admin.initializeApp ,
This gives the error
firebase-admin already initialized
.To fix we need to initialize only once, so initializing outside before calling the sendPushNotification().
after modifying all this it is now working fine.
Feature added :
added a feature to pass additional
data
payload within the message with the notification.Have you read the Contributing Guidelines on issues?
Yes