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

FE - 1.25: User Messaging Permission #86

Open
Ron-Madan opened this issue Jan 15, 2025 · 0 comments
Open

FE - 1.25: User Messaging Permission #86

Ron-Madan opened this issue Jan 15, 2025 · 0 comments
Assignees

Comments

@Ron-Madan
Copy link
Contributor

Ron-Madan commented Jan 15, 2025

In order for Firebase Cloud Messaging (FCM) to work, the user must first give permission.

So, after the user has signed in, they should be redirected to your new "Messaging" page. Here, you will make a clean/simple UI where you'll inform the user that these browser notifications will be really useful in letting you know your turn is here, etc.

**If the user declines or any error pops up, the token should be saved as "NULL"!

The following snippet will guide you through using Firebase messages:

import { getMessaging, getToken } from 'firebase/messaging';

const messaging = getMessaging();

const requestPermission = async () => {
  try {
    const permission = await Notification.requestPermission();
    if (permission === 'granted') {
      console.log('Notification permission granted.');
    } else {
      console.error('Notification permission denied.');
    }
  } catch (error) {
    console.error('Error requesting notification permission:', error);
  }
};

requestPermission();

Then, once permission is granted, FCM will generate a unique registration token that represents the user’s device or browser instance. We will use this token later on to send messages.

NOTE: The VAPID_KEY is available on the dashboard. I have also listed it here:

BIn7HfgGhmz1NJ7T0SOYcxQC1MkNjRlwT4awKTSJp9yvruJFZxzShp4reOdk1kpeKO6CRI2d68F0X_Dr1zrhfSI

const getFcmToken = async () => {
  try {
    const token = await getToken(messaging, { vapidKey: 'YOUR_PUBLIC_VAPID_KEY' });
    if (token) {
      console.log('FCM token:', token);
      // TODO! - Save to react context / local storage
    } else {
      console.error('Failed to get FCM token.');
    }
  } catch (error) {
    console.error('Error getting FCM token:', error);
  }
};

getFcmToken();

Now, modify the react context to include a token property as well. Then, once the token has been retrieved, you must save it to the context/local storage.

@Ron-Madan Ron-Madan changed the title FE - 1: User Messaging Permission FE - 1.25: User Messaging Permission Jan 15, 2025
@a1-su a1-su self-assigned this Jan 26, 2025
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

No branches or pull requests

2 participants