1
+ <!DOCTYPE html>
2
+ < html lang ="en ">
3
+ < head >
4
+ < meta charset ="UTF-8 ">
5
+ < title > Title</ title >
6
+ </ head >
7
+ < body >
8
+ < h2 > Firebase Web Push Notification</ h2 >
9
+
10
+ < p id ="token "> </ p >
11
+ < script src ="https://www.gstatic.com/firebasejs/7.14.6/firebase-app.js "> </ script >
12
+ < script src ="https://www.gstatic.com/firebasejs/7.14.6/firebase-messaging.js "> </ script >
13
+ < script >
14
+ var firebaseConfig = {
15
+ apiKey : "YOUR_API_KEY" ,
16
+ authDomain : "YOUR_FIREBASE_DOMAIN_NAME" ,
17
+ databaseURL : "YOUR_FIREBASE_DATBASE_URL" ,
18
+ projectId : "YOUR_FIREBASE_PROJECT_ID" ,
19
+ storageBucket : "YOUR_FIREBASE_STORAGE_BUCKET END WITH appspot.com" ,
20
+ messagingSenderId : "YOUR SENDER ID" ,
21
+ appId : "YOUR APP ID" ,
22
+ measurementId : "YOUR MEASUREMENT ID"
23
+ } ;
24
+ firebase . initializeApp ( firebaseConfig ) ;
25
+ const messaging = firebase . messaging ( ) ;
26
+
27
+ function IntitalizeFireBaseMessaging ( ) {
28
+ messaging
29
+ . requestPermission ( )
30
+ . then ( function ( ) {
31
+ console . log ( "Notification Permission" ) ;
32
+ return messaging . getToken ( ) ;
33
+ } )
34
+ . then ( function ( token ) {
35
+ console . log ( "Token : " + token ) ;
36
+ document . getElementById ( "token" ) . innerHTML = token ;
37
+ } )
38
+ . catch ( function ( reason ) {
39
+ console . log ( reason ) ;
40
+ } ) ;
41
+ }
42
+
43
+ messaging . onMessage ( function ( payload ) {
44
+ console . log ( payload ) ;
45
+ const notificationOption = {
46
+ body :payload . notification . body ,
47
+ icon :payload . notification . icon
48
+ } ;
49
+
50
+ if ( Notification . permission === "granted" ) {
51
+ var notification = new Notification ( payload . notification . title , notificationOption ) ;
52
+
53
+ notification . onclick = function ( ev ) {
54
+ ev . preventDefault ( ) ;
55
+ window . open ( payload . notification . click_action , '_blank' ) ;
56
+ notification . close ( ) ;
57
+ }
58
+ }
59
+
60
+ } ) ;
61
+ messaging . onTokenRefresh ( function ( ) {
62
+ messaging . getToken ( )
63
+ . then ( function ( newtoken ) {
64
+ console . log ( "New Token : " + newtoken ) ;
65
+ } )
66
+ . catch ( function ( reason ) {
67
+ console . log ( reason ) ;
68
+ } )
69
+ } )
70
+ IntitalizeFireBaseMessaging ( ) ;
71
+ </ script >
72
+ </ body >
73
+ </ html >
0 commit comments