Skip to content

Commit c71c40a

Browse files
committed
setup project
1 parent 83550fc commit c71c40a

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

firebase-messaging-sw.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
importScripts('https://www.gstatic.com/firebasejs/7.14.6/firebase-app.js');
2+
importScripts('https://www.gstatic.com/firebasejs/7.14.6/firebase-messaging.js');
3+
4+
var firebaseConfig = {
5+
apiKey: "YOUR_API_KEY",
6+
authDomain: "YOUR_FIREBASE_DOMAIN_NAME",
7+
databaseURL: "YOUR_FIREBASE_DATBASE_URL",
8+
projectId: "YOUR_FIREBASE_PROJECT_ID",
9+
storageBucket: "YOUR_FIREBASE_STORAGE_BUCKET END WITH appspot.com",
10+
messagingSenderId: "YOUR SENDER ID",
11+
appId: "YOUR APP ID",
12+
measurementId: "YOUR MEASUREMENT ID"
13+
};
14+
15+
firebase.initializeApp(firebaseConfig);
16+
const messaging=firebase.messaging();
17+
18+
messaging.setBackgroundMessageHandler(function (payload) {
19+
console.log(payload);
20+
const notification=JSON.parse(payload);
21+
const notificationOption={
22+
body:notification.body,
23+
icon:notification.icon
24+
};
25+
return self.registration.showNotification(payload.notification.title,notificationOption);
26+
});

notification.html

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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>

send_notification.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
function sendNotification(){
3+
$url ="https://fcm.googleapis.com/fcm/send";
4+
5+
$fields=array(
6+
"to"=>$_REQUEST['token'],
7+
"notification"=>array(
8+
"body"=>$_REQUEST['message'],
9+
"title"=>$_REQUEST['title'],
10+
"icon"=>$_REQUEST['icon'],
11+
"click_action"=>"https://google.com"
12+
)
13+
);
14+
15+
$headers=array(
16+
'Authorization: key=YOUR_SERVER_KEY',
17+
'Content-Type:application/json'
18+
);
19+
20+
$ch=curl_init();
21+
curl_setopt($ch,CURLOPT_URL,$url);
22+
curl_setopt($ch,CURLOPT_POST,true);
23+
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
24+
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
25+
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($fields));
26+
$result=curl_exec($ch);
27+
print_r($result);
28+
curl_close($ch);
29+
}
30+
sendNotification();

0 commit comments

Comments
 (0)