我正试图在我的Nuxt.js应用程序中实现谷歌的Firebase消息传递。
到目前为止,我已经安装了firebase,在./plugins文件夹中创建了一个firebase.js插件,导入和初始化了防火墙和消息传递服务,一切看起来都很好。
现在我不知道该怎么走,也不知道该怎么走..
其想法是在通知模块中处理vuex内部的所有内容。
我想同时处理背景和前景通知。后台由服务工作者处理,对于前台,我做了一个简单的通知组件,每当我收到FCM的推送通知时,我都想显示它。
问题:
如何注册服务工作者、请求许可和处理前台/后台通知?我是说特定于Nuxt.js的确切位置/文件/方式?我是否应该为此制作另一个插件,使用中间件文件夹,还是只处理默认布局文件中的所有内容?
最干净的方法是什么?
提前感谢!
发布于 2021-04-29 08:32:34
步骤1)安装依赖项
npm install firebase
npm install @nuxtjs/firebase步骤2)在项目的根文件夹上创建一个文件serviceWorker.js。
self.addEventListener('push', function (e) {
data = e.data.json();
var options = {
body: data.notification.body,
icon: data.notification.icon,
vibrate: [100, 50, 100],
data: {
dateOfArrival: Date.now(),
primaryKey: '2'
},
};步骤3)按照以下步骤控制您的nuxt.config.js。
将这一行添加到文件的顶部。
const fs = require('fs')使用firebase凭据更新模块数组。
[
'@nuxtjs/firebase',
{
config: {
apiKey: "<yourKey>",
authDomain: "<yourAuthDomain>",
projectId: "<yourProjectId>",
storageBucket: "<yourStorageBucket>",
messagingSenderId: "<yourMessagingSenderId>",
appId: "<yourAppId>",
measurementId: ",<yourMeasurementId>"
},
onFirebaseHosting: false,
services: {
messaging: {
createServiceWorker: true,
fcmPublicVapidKey: '<yourFCMPublicVapidKey>',
inject: fs.readFileSync('./serviceWorker.js')
}
}
}
]第4步>最后。到您的index.js或布局文件。
async mounted() {
const currentToken = await this.$fire.messaging.getToken()
const data = JSON.stringify({
notification: {
title: 'firebase',
body: 'firebase is awesome',
click_action: 'http://localhost:3000/',
icon: 'http://localhost:3000/assets/images/brand-logo.png'
},
to: currentToken
})
const config = {
method: 'post',
url: 'https://fcm.googleapis.com/fcm/send',
headers: {
'Content-Type': 'application/json',
'Authorization': 'key=<yourServerKey>'
},
data
};
const response = await axios(config)
this.$fire.messaging.onMessage((payload) => {
console.info('Message received: ', payload)
})
this.$fire.messaging.onTokenRefresh(async () => {
const refreshToken = await this.$fire.messaging.getToken()
console.log('Token Refreshed',refreshToken)
})
}要了解更多细节,您可以访问这个文章。
发布于 2021-02-08 22:42:59
您可以检查Nuxt模块Nuxt Firebase https://firebase.nuxtjs.org
这些文档是非常好的https://firebase.nuxtjs.org/service-options/messaging
发布于 2021-03-11 02:22:57
一旦您安装了@nuxtjs/firebase模块并将以下代码插入到您的nuxt.config.js中,您就可以从firebase控制台获得这些数据。我将它们作为一个dotenv模块放置,因为管理不同项目的配置模板更容易。
firebase: {
config: {
apiKey: dotenv.parsed.apiKey,
authDomain: dotenv.parsed.authDomain,
databaseURL: dotenv.parsed.databaseURL,
projectId: dotenv.parsed.projectId,
storageBucket: dotenv.parsed.storageBucket,
messagingSenderId: dotenv.parsed.messagingSenderId,
appId: dotenv.parsed.appId,
measurementId: dotenv.parsed.measurementId
},
onFirebaseHosting: false,
services: {
messaging: {
createServiceWorker: true,
fcmPublicVapidKey: dotenv.parsed.vapidKey // OPTIONAL : Sets vapid key for FCM after initialization
}一旦您实现了它,模块将自动生成服务工作人员,您可以在检查元素控制台上查看它们。
都做完了然后说,
在您的vuex商店中,只需打电话给this.$fire.messaging.getToken(),询问用户接收通知的权限。
您可以在调用this.$fire.messaging.getToken()的下面使用以下函数启动接收消息
messaging.onMessage(function (payload) {
context.dispatch('yourDesireDispatchFunction', payload)
})https://stackoverflow.com/questions/58163755
复制相似问题