我已经使用 Vue-cli 3 创建了一个 Vue 应用程序,并且我一直在尝试将 FCM 合并到其中。但是,我已经研究了两天,但仍然无法使其正常工作。
首先,这是我的
importScripts('https://www.gstatic.com/firebasejs/4.8.1/firebase- app.js');
importScripts('https://www.gstatic.com/firebasejs/4.8.1/firebase-messaging.js');
var config = {
messagingSenderId: "69625964474"
};
firebase.initializeApp(config);
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function (payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload)
// Customize notification here
const notificationTitle = 'Background Message Title';
const notificationOptions = {
body: 'Background Message body.',
icon: '/firebase-logo.png'
}
return self.registration.showNotification(notificationTitle, notificationOptions)
});
Run Code Online (Sandbox Code Playgroud)
``
sorta 工作的一种解决方案是我将此文件移动到文件public夹中并App.vue使用
const registration = await navigator.serviceWorker.register(`${process.env.BASE_URL}firebase-messaging-sw.js`)
messaging.useServiceWorker(registration)
Run Code Online (Sandbox Code Playgroud)
但是,那么我将有两个 service worker(另一个来自 Vue 本身)。
我尝试vue.config.js通过添加以下配置来修改而不是尝试使用 Workbox: …
我正在阅读JavaScript Ninja的秘密,并遇到了一个编码示例:
var ninja = {
yell: function(n){
return n > 0 ? ninja.yell(n-1) + "a" : "hiy";
}
};
var samurai = { yell: ninja.yell };
//var ninja = {};
//delete ninja;
samurai.yell(4);
Run Code Online (Sandbox Code Playgroud)
如果我取消注释第二个注释行,samurai.yell(4)运行正常,我理解它samurai仍然有它的引用最初引用的函数ninja.yell,delete删除.
但是,如果我改为运行第一个注释行,samurai.yell(4)则会出错.
有人可以解释引擎盖下发生了什么吗?
我想如果你指定ninja为be undefined,将创建一个新的引用并链接到ninja,原始引用仍然存在,并且由于samurai仍然有对该函数的引用,因此垃圾收集不会出现并在内存中将其删除.
我正在学习Redux,我可以看到人们将各种信息存储到状态,并通过reducer进行各种不同的操作.但是Redux是安全的还是使用Redux有任何已知的漏洞?如果有,那么我可以采取哪些最佳做法来保障国家安全?
我有一个在公司代理后面工作的 NodeJS API,我一直在尝试使用他们的 NodeJS 客户端与 Google 进行身份验证:
const google = require('googleapis');
function getServiceAccountInfo() {
return {
type: 'service_account',
project_id: 'XXXXX',
private_key_id: 'XXXXXX',
private_key: 'XXXX',
client_email: 'XXXX',
client_id: 'XXXX',
auth_uri: 'XXXX',
token_uri: 'XXXX',
auth_provider_x509_cert_url: 'XXXX',
client_x509_cert_url: 'XXXXX'
};
}
const SCOPES = 'https://www.googleapis.com/auth/firebase.messaging';
let proxy2 = {
host: 'proxy.hkcsl.net',
port: 8080
};
const proxy3 = {
proxy: 'http://172.25.2.6:8080'
}
const proxy4 = {
proxy: {
host: '172.25.2.6',
port: 8080,
auth: {
username: '',
password: '',
},
}
}
process.env.HTTPS_PROXY = 'https://172.25.2.6:8080'; …Run Code Online (Sandbox Code Playgroud) 我现在正在学习使用Class语法创建React组件,并注意我现在必须声明这样的方法:
class Foo extends React.Component {
...
bar = () => {
this.setState({ ... })
}
}
Run Code Online (Sandbox Code Playgroud)
而不是像这样:
class Foo extends React.Component {
...
bar() {
this.setState({ ... }) // won't work
}
}
Run Code Online (Sandbox Code Playgroud)
或者我不能用this.setState().
任何人都可以解释创建这样的方法与它们与函数原型的关系有何区别?
我一直在玩 Google+ API 并尝试使用以下网址获取 Google+ 用户的个人资料图片网址:
https://www.googleapis.com/plus/v1/people/ {user_id}?key={API_KEY}
不需要 OAuth,您也可以在这里尝试一下(不需要 API 密钥):
起初我使用 Fetch API 来获取数据,因为我也想使用 service worker 来做到这一点:
fetch('https://www.googleapis.com/plus/v1/people/116725099929439898086?key=MY_API_KEY')
.then(function(response){
console.log(response);
});
Run Code Online (Sandbox Code Playgroud)
但它只给了我这个回应:
{
body: ReadableStream
bodyUsed: false
headers: Headers
ok: true
status: 200
statusText: ""
type: "cors"
url: "https://www.googleapis.com/plus/v1/people/115681458968227650592?key=MY_API_KEY"
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我改用 jQuery 的 getJSON 方法:
$.getJSON( "https://www.googleapis.com/plus/v1/people/115681458968227650592?key=MY_API_KEY", function( data ) {
console.log(data);
});
Run Code Online (Sandbox Code Playgroud)
它就像一个魅力,我可以得到我需要的东西:
{
"kind": "plus#person",
"etag": "\"FT7X6cYw9BSnPtIywEFNNGVVdio/DgskSQn7XXHCvjcdFBFkiqEbsfo\"",
"gender": "male",
"urls": [ ... ],
"objectType": "person",
"id": "116725099929439898086",
"displayName": "Kevin Lai",
...
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么他们会做出如此不同的行为吗?而且,如何在仍然使用 Fetch API …
我一直在尝试使用以下正文通过API调用(POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send)向订阅了多个主题的用户发送云消息:
{
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer MY_ACCESS_TOKEN`
},
body: {
"message": {
"condition": "'cat' in topics || 'dog' in topics",
"notification": {
"title": "Message Title",
"body": "Message Body"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我根据FCM文档编写了这个配置.我可以得到类似的回复:
{
"name": "projects/myproject-b5ae1/messages/5735743068807585451"
}
Run Code Online (Sandbox Code Playgroud)
但是没有任何设备收到任何消息.
我已经将访问令牌用于其他API,这有效.我也知道FCM文档也为cURL请求提供了不同的主体,我也试过但不行.
奇怪的是,如果我只提供一个主题,则condition消息实际上已成功发送到订阅的设备.我写错了吗?
我已经在Stackoverflow上引用了其他解决方案,但一切仍然无效:
有人能告诉我为什么这两个函数会产生不同的输出吗?为什么一个给'未定义'而另一个给'对象'?
function aaa(){
return
{
test: 1
};
}
console.log(typeof aaa());
function abc(){
return {test: 1};
}
console.log(typeof abc());Run Code Online (Sandbox Code Playgroud)