lambrohan / strapi-firebase-auth

Learn how to implement firebase authentication into strapi.
33 stars 5 forks source link

Regarding sending the push issues #2

Closed ShreyashSalian15 closed 2 years ago

ShreyashSalian15 commented 2 years ago

Could me please guide us on how to send the notification from the Strapi to the firebase user?. I have created the collection name as a notification and had given relation to the Firebase user collection. So when the admin enters the details in the notification collection it should send the notification message to the firebase user which is there in the Firebase user collection. I have tried from my side. But I facing issues. I am not able to understand where to write the code for the push notification.

lambrohan commented 2 years ago

What is the strapi version you are using?

On Wed, 11 May 2022, 10:22 am ShreyashSalian15, @.***> wrote:

Could me please guide us on how to send the notification from the Strapi to the firebase user?. I have created the collection name as a notification and had given relation to the Firebase user collection. So when the admin enters the details in the notification collection it should send the notification message to the firebase user which is there in the Firebase user collection. I have tried from my side. But I facing issues. I am not able to understand where to write the code for the push notification.

— Reply to this email directly, view it on GitHub https://github.com/lambrohan/strapi-firebase-auth/issues/2, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHGJK6J566B2OWZZN7QAUXDVJM4JPANCNFSM5VTTUDRQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

ShreyashSalian15 commented 2 years ago

Sorry Now, I am working on Strapi version 3. In future I want to upgrade version 4.

lambrohan commented 2 years ago

This guide will work with version 3 but I'm not sure if it will work on version 4. I'm planning to make a guide or a plugin rather to achieve this implementation easily.

As per this guide you can use the firebase instance injected globally to send the notifications.

     await strapi.firebase.messaging().send({
        notification: {
          title: "NOTIFICATION TITLE",
          body: "NOTIFICATION_MESSAGE",
        },
        token: "FCM_TOKEN",
      });
ShreyashSalian15 commented 2 years ago

Thank you very much for your help. I will try it.

ShreyashSalian15 commented 2 years ago

Could you please help me where to implement this above code in the Strapi project and what we should write in the token while passing the notification

lambrohan commented 2 years ago

To manage the token -

  1. Add a field called fcmToken to your firebase-user collection.
  2. When your device generates the token update the value with update api.

To send notification - It depends on the event you want to trigger the notification. eg. if you want to send a notification after a collection is created, updated you can use the model lifecycle hooks to implement this. https://strapi.gitee.io/documentation/v3.x/concepts/models.html#lifecycle-hooks

use strict";

/**
 * Read the documentation (https://strapi.io/documentation/developer-docs/latest/concepts/models.html#lifecycle-hooks)
 * to customize this model
 */

module.exports = {
  lifecycles: {
    async afterCreate(result, data) {
      var registrationToken = result.user.fcmToken;

      var message = {
        notification: {
          title: result.title,
          body: result.message,
        },
        token: registrationToken,
      };

      // Send a message to the device corresponding to the provided
      // registration token.
      try {
        await strapi.firebase.messaging().send(message);
      } catch (error) {
        console.log("Error sending message:", error.response);
      }
    },
  },
};
ShreyashSalian15 commented 2 years ago

Thanks for your help really appreciate it. But while implementing this code I am facing some issues. I have attached the screenshot below. image

lambrohan commented 2 years ago

I think there is something wrong with the registration token you are passing. You can check the latest docs to investigate more https://firebase.google.com/docs/reference/admin/node/firebase-admin.messaging.messaging.md#messagingsend

You can test it using hardcoded title, message and token and see if notifications gets delivered.

ShreyashSalian15 commented 2 years ago

I have checked the doc and have passed the token but we are still facing this error. Please refer to the screenshot and help me. I have checked the token which we have passed in that it's working and we are able to pass the notification using another CMS. So please guide me on what we need to do next for that. Hope you can understand.

lambrohan commented 2 years ago

I think you have to investigate the tokens and device for that. Also these tokens expires after a period so you have to make sure to use the correct ones.

Either you are using the wrong token or you are initialising the firebase with wrong config.

ShreyashSalian15 commented 2 years ago

Thank you so much. I will work on it.

ShreyashSalian15 commented 2 years ago

Hello, lambrohan. I am successfully sending the notification and thank you for your help. But If I want to send the notification at a particular date and time what function should I use. Can you please help me in that case?. And I have added the data field in the collection with the DateTime datatype.

lambrohan commented 2 years ago

You can either use strapi cron jobs or implement a queue with packages such as bull