I am using @nuxtjs/firebase in a nuxt project. The messaging works, I can send messages from postman via fcm to my website. I can't see any documentation on how I can access the subscribeToTopic.
In my nuxt.config.js I have:
[
'@nuxtjs/firebase',
{
config: {
apiKey: "myapiKey",
authDomain: "myDomain",
projectId: "myProjectId",
storageBucket: "myStorageBucker",
messagingSenderId: "mySenderId",
appId: "myAppId",
measurementId: "myMeasurementId"
},
services: {
auth: {
persistence: 'local',
initialize: {
onAuthStateChangedAction: 'onAuthStateChangedAction',
subscribeManually: false
},
ssr: false
},
messaging: {
createServiceWorker: true,
fcmPublicVapidKey: 'myVapidKey',
inject: fs.readFileSync('./serviceWorker.js')
},
firestore: true
}
}
]
serviceWorker.js has:
self.addEventListener('push', function (e) {
const data = e.data.json();
const options = {
body: data.notification.body,
icon: data.notification.icon,
vibrate: [100, 50, 100],
data: {
dateOfArrival: Date.now(),
primaryKey: '2'
},
};
})
In async mounted I have:
const currentToken = await this.$fire.messaging.getToken()
console.log("token", currentToken)
this.$fire.messaging.onMessage((payload) => {
console.info('Message received. ', payload)
this.$toast.success(payload.data.text + "</br>" + payload.data.additionalInfo, { icon: payload.data.icon });
})
this.$fire.messaging.onTokenRefresh(async () => {
const refreshToken = await this.$fire.messaging.getToken()
console.log('Token Refreshed', refreshToken)
})
This all works as expected.
Apparently I should be able to subscribe to topics, in firebase something like the following can be used:
FirebaseMessaging.getInstance().subscribeToTopic("all")
.addOnCompleteListener { task ->
if (task.isSuccessful) {
//Successfully Subscribed to Notification Service
}else{
//Subscription Notification Service failed
}
}
But, I cannot see how to do that using @nuxtjs/firebase module.
There is no client-side API in the JavaScript SDK to subscribe to a topic. You'll instead need to use a (server-side) Admin SDK to accomplish this.
For example, the
subscribeToTopicmethod you refer to exists in the Node.js SDK and in the Java SDK, but not in the JavaScript/Web or Android SDKs.I recommend studying the documentation on subscribing a web client to a topic a bit further.
Once you subscribe a client to a topic from the server, the existing client-side code you already have should start receiving messages to that topic too.