Skip to content
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

Merged
merged 5 commits into from
May 28, 2024

Conversation

Snehasis4321
Copy link
Contributor

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

Copy link
Contributor

@loks0n loks0n left a 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);
Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this comment

Comment on lines 4 to 10
throwIfMissing(process.env, [
'FCM_PROJECT_ID',
'FCM_PRIVATE_KEY',
'FCM_CLIENT_EMAIL',
'FCM_DATABASE_URL',
]);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert this

Copy link
Contributor

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}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert this

Comment on lines 13 to 20
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,
});}
Copy link
Contributor

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)}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this comment

@Snehasis4321
Copy link
Contributor Author

Snehasis4321 commented May 8, 2024

Done with all the requested changes. Can you please review my code? @loks0n.

@Snehasis4321 Snehasis4321 reopened this May 9, 2024
@Snehasis4321 Snehasis4321 requested a review from loks0n May 9, 2024 05:10
Comment on lines 4 to 10
throwIfMissing(process.env, [
'FCM_PROJECT_ID',
'FCM_PRIVATE_KEY',
'FCM_CLIENT_EMAIL',
'FCM_DATABASE_URL',
]);

Copy link
Contributor

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

@Snehasis4321
Copy link
Contributor Author

moved throwIfMissing to the top of the function.

@Snehasis4321 Snehasis4321 requested a review from loks0n May 22, 2024 14:42
@loks0n loks0n merged commit 882f67b into appwrite:main May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants