-
Notifications
You must be signed in to change notification settings - Fork 1
/
admin.html
147 lines (139 loc) · 4.95 KB
/
admin.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<!DOCTYPE html>
<html>
<head>
<title>Admin - August FCM web push demo code - August - Let's Write</title>
<link rel="canonical" href="https://www.letswrite.tw/pwa-web-push/">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="shortcut icon" href="https://letswritetw.github.io/letswritetw/dist/img/logo_512.png"/>
<!-- Google Tag Manager-->
<script>
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-PGQ9WQT');
</script>
</head>
<body>
<!-- Google Tag Manager (noscript)-->
<noscript>
<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-PGQ9WQT" height="0" width="0" style="display:none;visibility:hidden"></iframe>
</noscript>
<div id="app">
<form>
<div>
<label for="title">標題</label>
<input id="title" type="text" v-model="form.title">
</div>
<div>
<label for="description">內文</label>
<input id="description" type="text" v-model="form.body">
</div>
<div>
<label for="link">連結</label>
<input id="link" type="text" v-model="form.url">
</div>
<div>
<label for="utm_source">utm_source</label>
<input id="utm_source" type="text" v-model="form.utmS">
</div>
<div>
<label for="utm_medium">utm_medium</label>
<input id="utm_medium" type="text" v-model="form.utmM">
</div>
<div>
<label for="utm_campaign">utm_campaign</label>
<input id="utm_campaign" type="text" v-model="form.utmC">
</div>
<button type="button" @click.prevent="push">發送</button>
</form>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.19/vue.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.7.0/firebase.js"></script>
<script>
var config = {
apiKey: '',
authDomain: 'xxxxx.firebaseapp.com',
databaseURL: 'https://xxxxx.firebaseio.com',
projectId: 'xxxxx',
storageBucket: 'xxxxx.appspot.com',
messagingSenderId: 'xxxxxxx'
};
firebase.initializeApp(config);
var db = firebase.database();
var messaging = firebase.messaging();
messaging.usePublicVapidKey('從firebase上取得的FCM金鑰');
var pushMsg = new Vue({
el: '#app',
data: {
tokens: [],
key: '從firebase上取得伺服器金鑰',
form: {
title: 'web push標題',
body: 'web push內文',
url: 'https://www.domain.com.tw',
utmS: 'webpush',
utmM: 'webpush',
utmC: 'webpush'
}
},
methods: {
// web push
push: function() {
var notification = {
'title': this.form.title,
'body': this.form.body,
'badge': '/logo/logo192.png',
'icon': '/logo/logo192.png',
'click_action': this.url(),
'data': {
'url': this.url()
}
};
// 發送內容存至firebase
var d = new Date();
var y = d.getFullYear(),
m = d.getMonth() + 1,
day = d.getDate(),
hour = d.getHours(),
min = d.getMinutes();
var time = y + '-' + m + '-' + day + '--' + hour + ':' + min;
db.ref('pushMessages/' + time).set(notification);
// push
var _this = this;
this.tokens.forEach(function(to) {
fetch('https://fcm.googleapis.com/fcm/send', {
'method': 'POST',
'headers': {
'Authorization': 'key=' + _this.key,
'Content-Type': 'application/json'
},
'body': JSON.stringify({
'data': notification,
'to': to,
})
}).then(function(response) {
console.log(response);
}).catch(function(error) {
alert(error);
});
});
}
},
mounted: function() {
// 抓所有firebase裡的token
db.ref('/pushUsers/').once('value').then(snapshot => {
var lists = snapshot.val();
var t = [];
Object.keys(lists).map((key, index) => {
var value = lists[key];
this.tokens.push(value.token);
});
});
},
});
</script>
</body>
</html>