Push Notification in Android using Firebase (FCM) and PHP
- Get link
- X
- Other Apps
Creating PHP Project
1. Start WAMP/XAMPP server and navigate to www or htdocs folder and create a folder named firebase inside it. Inside this folder, create a subfolder called notifications
2. Now create a file named notification.php inside notifications folder. This will have Notification class which will help in constructing the notification data payload.
notification.php
PHP
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
|
<?php
class Notification{
private $title;
private $message;
private $image_url;
private $action;
private $action_destination;
private $data;
function __construct(){
}
public function setTitle($title){
$this->title = $title;
}
public function setMessage($message){
$this->message = $message;
}
public function setImage($imageUrl){
$this->image_url = $imageUrl;
}
public function setAction($action){
$this->action = $action;
}
public function setActionDestination($actionDestination){
$this->action_destination = $actionDestination;
}
public function setPayload($data){
$this->data = $data;
}
public function getNotificatin(){
$notification = array();
$notification['title'] = $this->title;
$notification['message'] = $this->message;
$notification['image'] = $this->image_url;
$notification['action'] = $this->action;
$notification['action_destination'] = $this->action_destination;
return $notification;
}
}
?>
|
3. Now create index.php and add the following content. This contains the form to fill notification details and CURL request, that is made to the Firebase API.
index.php
PHP
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
<!DOCTYPE html>
<html lang="en">
<head>
<title>Firebase Push Notification</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-lg-6">
<h2>Send Firebase Push Notification | AndroidDeft.Com</h2>
<hr />
<form action="" method="post">
<div class="form-group">
<label for="send_to">Send To:</label>
<select name="send_to" id="send_to" class="form-control">
<option value="sngle">Single Device</option>
<option value="topic">Topic</option>
</select>
</div>
<div class="form-group">
<label for="firebase_api">Firebase Server API Key:</label>
<input type="text" required="" class="form-control" id="firebase_api" placeholder="Enter Firebase Server API Key" name="firebase_api">
</div>
<div class="form-group" id="firebase_token_group">
<label for="firebase_token">Firebase Token:</label>
<input type="text" required="" class="form-control" id="firebase_token" placeholder="Enter Firebase Token" name="firebase_token">
</div>
<div class="form-group" style="display: none" id="topic_group">
<label for="topic">Topic Name:</label>
<input type="text" class="form-control" id="topic" placeholder="Enter Topic Name" name="topic">
</div>
<div class="form-group">
<label for="title">Title:</label>
<input type="text" required="" class="form-control" id="title" placeholder="Enter Notification Title" name="title">
</div>
<div class="form-group">
<label for="message">Message:</label>
<textarea required="" class="form-control" rows="5" id="message" placeholder="Enter Notification Message" name="message"></textarea>
</div>
<div class="checkbox">
<label><input type="checkbox"id="include_image" name="include_image">Include Image</label>
</div>
<div class="form-group" style="display: none" id="image_url_group">
<label for="image_url">Image URL:</label>
<input type="url" class="form-control" id="image_url" placeholder="Enter Image URL" name="image_url">
</div>
<div class="checkbox">
<label><input type="checkbox" id="include_action" name="include_action">Include Action</label>
</div>
<div class="form-group" style="display: none" id="action_group">
<label for="action">Action:</label>
<select name="action" id="action" class="form-control">
<option value="url">Open URL</option>
<option value="activity">Open Activity</option>
</select>
</div>
<div class="form-group" style="display: none" id="action_destination_group">
<label for="action_destination">Destination:</label>
<input type="text" class="form-control" id="action_destination" placeholder="Enter Destination URL or Activity name" name="action_destination">
</div>
<button type="submit" class="btn btn-info">Submit</button>
</form>
</div>
<div class="col-lg-6">
<?php
if(isset($_POST['title'])){
require_once __DIR__ . '/notification.php';
$notification = new Notification();
$title = $_POST['title'];
$message = isset($_POST['message'])?$_POST['message']:'';
$imageUrl = isset($_POST['image_url'])?$_POST['image_url']:'';
$action = isset($_POST['action'])?$_POST['action']:'';
$actionDestination = isset($_POST['action_destination'])?$_POST['action_destination']:'';
if($actionDestination ==''){
$action = '';
}
$notification->setTitle($title);
$notification->setMessage($message);
$notification->setImage($imageUrl);
$notification->setAction($action);
$notification->setActionDestination($actionDestination);
$firebase_token = $_POST['firebase_token'];
$firebase_api = $_POST['firebase_api'];
$topic = $_POST['topic'];
$requestData = $notification->getNotificatin();
if($_POST['send_to']=='topic'){
$fields = array(
'to' => '/topics/' . $topic,
'data' => $requestData,
);
}else{
$fields = array(
'to' => $firebase_token,
'data' => $requestData,
);
}
// Set POST variables
$url = 'https://fcm.googleapis.com/fcm/send';
$headers = array(
'Authorization: key=' . $firebase_api,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarily
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if($result === FALSE){
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo '<h2>Result</h2><hr/><h3>Request </h3><p><pre>';
echo json_encode($fields,JSON_PRETTY_PRINT);
echo '</pre></p><h3>Response </h3><p><pre>';
echo $result;
echo '</pre></p>';
}
?>
</div>
</div>
</div>
<script>
$('#include_image').change(function(e){
if($(this).prop("checked")==true){
$('#image_url_group').show();
$("#image_url").prop('required',true);
}else{
$('#image_url_group').hide();
$("#image_url").prop('required',false);
}
});
$('#include_action').change(function(e){
if($(this).prop("checked")==true){
$('#action_group').show();
$('#action_destination_group').show();
$("#action_destination").prop('required',true);
}else{
$('#action_group').hide();
$('#action_destination_group').hide();
$("#action_destination").prop('required',false);
}
});
$('#send_to').change(function(e){
var selectedVal = $("#send_to option:selected").val();
if(selectedVal=='topic'){
$('#topic_group').show();
$("#topic").prop('required',true);
$('#firebase_token_group').hide();
$("#firebase_token").prop('required',false);
}else{
$('#topic_group').hide();
$("#topic").prop('required',false);
$('#firebase_token_group').show();
$("#firebase_token").prop('required',true);
}
});
</script>
</body>
</html>
|
4. Now go to http://localhost/firebase/notifications/ in your browser and access the PHP console. If you have any port configured to Apache, then don’t forget to use it.
5. You can post notifications from here: Demo Console. You need not to worry about using your own Firebase API key. We do not have any logic to access or store them.
- Get link
- X
- Other Apps
Nice Article, Blog theme is also very user friendly. Tech information is also good on this blog. Also checkout - Push Notification in App
ReplyDeleteThanks