Home > Blockchain >  How do I register new vapidKeys outside FCM console UI
How do I register new vapidKeys outside FCM console UI

Time:02-03

Update

As Gerardo pointed out, vapidKey != FCM Token.

I finally got things going, though im unsure if this is the best way to do it.

This was what i needed to generate a NEW token:

try{
            if(currentToken) await deleteToken(messaging,currentToken)
            if(Notification.permission !== 'granted') await Notification.requestPermission();
            currentToken = await getToken(messaging,vapidKey);
            $.ajax({
                type:"GET",
                url:"/fcm/register-token",
                data:{token:currentToken},
                success:(saved)=>{
                    if(saved) console.log(saved,' New token registered ',currentToken)
                    else console.log('Unable to save token!')
                }
            })
        }
        catch(e){console.log(e)}

Question

I am testing FCM for push notifications in javascript. I have a single "Web Push Certificate" (vapidKey) generated manually in the FCM console UI. This works for sending a test notification to a single token, but i am unclear on how any additional users would be able to register a new key with FCM. I must be misunderstanding something fundamental here, and am not even sure what to be searching for.

// TODO: Find out how to generate new registration token as needed
const vapidKey = {vapidKey:"BLgq_6FY0suqAy9llMyYX03o9K9n4FHba1gCnPz6KmG9oh6K8v3Ws4SIG9KTILfN0_8np9PfT5bPqgTDXfCA9dc"};

async function fcm_init(){
    try{
        // TODO: user interaction for notification permission
        await Notification.requestPermission()
        const currentToken = await getToken(messaging,vapidKey);
        // TODO: Send token to server to save in tbl_fcm
        console.log(currentToken);
    }
    catch(err){
        console.log(err);
    }
}fcm_init()

In this example, the vapidKey is hard-coded, but how would i go about getting a different key for each user in the app without manually doing so in the FCM Console UI?

This is where i have generated the key to get a token for a user. This is what im trying to avoid doing manually for each user on the system.

This is where i have generated the key to get a token for a user. This is what im trying to avoid doing manually for each user on the system.

CodePudding user response:

The vapidKey is needed for identifying the app (not the user). This is mentioned in the second screenshot "Firebase Cloud Messaging can use Application Identity key pairs to connect with external push services".

With this information, your app can generate an FCM Token (or registration token) in the client:

const currentToken = await getToken(messaging,vapidKey);

You can use these FCM tokens to send messages to a specific device. These FCM Tokens identify the device (user) and they will be different for each one.

Check this example on how you would send a message from the back end to a specific device using it's FCM token (registration token).

  • Related