-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathwp-firebase-push-notification.php
207 lines (162 loc) · 7.05 KB
/
wp-firebase-push-notification.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
205
206
207
<?php
/*
Plugin Name:Wordpress Firebase Push Notification
Description:Notify your users of new posts with Firebase cloud messaging (FCM)
Version:1
Author:sony7596, miraclewebssoft, reachbaljit
Author URI:http://www.miraclewebsoft.com
License:GPL2
License URI:https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('ABSPATH')) {
exit;
}
if (!defined("FCM_VERSION_CURRENT")) define("FCM_VERSION_CURRENT", '1');
if (!defined("FCM_URL")) define("FCM_URL", plugin_dir_url( __FILE__ ) );
if (!defined("FCM_PLUGIN_DIR")) define("FCM_PLUGIN_DIR", plugin_dir_path(__FILE__));
if (!defined("FCM_PLUGIN_NM")) define("FCM_PLUGIN_NM", 'Wordpress Firebase Push Notification');
if (!defined("FCM_TD")) define("FCM_TD", 'fcm_td');
Class Firebase_Push_Notification
{
public $pre_name = 'fcm';
public function __construct()
{
// Installation and uninstallation hooks
register_activation_hook(__FILE__, array($this, $this->pre_name . '_activate'));
register_deactivation_hook(__FILE__, array($this, $this->pre_name . '_deactivate'));
add_action('admin_menu', array($this, $this->pre_name . '_setup_admin_menu'));
add_action("admin_init", array($this, $this->pre_name . '_backend_plugin_js_scripts_filter_table'));
add_action("admin_init", array($this, $this->pre_name . '_backend_plugin_css_scripts_filter_table'));
add_action('admin_init', array($this, $this->pre_name . '_settings'));
add_action('publish_post', array($this, $this->pre_name . '_on_post_publish'), 10, 2);
//add_action('init', array($this, $this->pre_name . '_custom_post_type'));
}
public function fcm_setup_admin_menu()
{
add_submenu_page('options-general.php', __('Firebase Push Notification', FCM_TD), FCM_PLUGIN_NM, 'manage_options', 'fcm_slug', array($this, 'fcm_admin_page'));
add_submenu_page(null // -> Set to null - will hide menu link
, __('Test Notification', FCM_TD)// -> Page Title
, 'Test Notification' // -> Title that would otherwise appear in the menu
, 'administrator' // -> Capability level
, 'test_notification' // -> Still accessible via admin.php?page=menu_handle
, array($this, 'fcm_test_notification') // -> To render the page
);
}
public function fcm_admin_page()
{
include(plugin_dir_path(__FILE__) . 'views/dashboard.php');
}
function fcm_backend_plugin_js_scripts_filter_table()
{
wp_enqueue_script("jquery");
wp_enqueue_script("fcm.js", FCM_URL . "assets/js/fcm.js");
}
function fcm_backend_plugin_css_scripts_filter_table()
{
wp_enqueue_style("fcm.css", FCM_URL . "assets/css/fcm.css");
}
public function fcm_activate()
{
}
public function fcm_deactivate()
{
}
function fcm_settings()
{ //register our settings
register_setting('fcm_group', 'stf_fcm_api');
register_setting('fcm_group', 'fcm_option');
register_setting('fcm_group', 'fcm_topic');
register_setting('fcm_group', 'fcm_disable');
register_setting('fcm_group', 'fcm_update_disable');
register_setting('fcm_group', 'fcm_page_disable');
register_setting('fcm_group', 'fcm_update_page_disable');
}
function fcm_custom_post_type()
{
register_post_type('device_tokens',
[
'labels' => [
'name' => __('Device Tokens'),
'singular_name' => __('Device Token'),
],
'public' => true,
'has_archive' => true,
]
);
}
// https://wordpress.stackexchange.com/questions/247447/how-can-i-tell-if-a-post-has-been-published-at-least-once
function fcm_on_post_publish($post_id, $post) {
// $from = get_bloginfo('name');
// $content = 'There are new post notification from '.$from;
$content = $post->post_title;
if (get_option('stf_fcm_api') && get_option('fcm_topic')) {
$published_at_least_once = get_post_meta( $post_id, 'is_published', true );
if (!$published_at_least_once && get_option('fcm_disable') != 1) {
$published_at_least_once = true;
$this->fcm_notification($content, (string) $post_id);
}
update_post_meta( $post_id, 'is_published', $published_at_least_once );
}
}
function fcm_test_notification(){
$content = 'Test Notification from FCM Plugin';
$result = $this->fcm_notification($content, '0');
echo '<div class="row">';
echo '<div><h2>Debug Information</h2>';
echo '<pre>';
printf($result);
echo '</pre>';
echo '<p><a href="'. admin_url('admin.php').'?page=test_notification">Retry</a></p>';
echo '<p><a href="'. admin_url('admin.php').'?page=fcm_slug">Home</a></p>';
echo '</div>';
}
function fcm_notification($content, $post_id){
$topic = "'".get_option('fcm_topic')."' in topics";
$apiKey = get_option('stf_fcm_api');
$url = 'https://fcm.googleapis.com/fcm/send';
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
$notification_data = array(
// when application open then post field 'data' parameter work so 'message' and 'body' key should have same text or value
'message' => $content,
'post_id' => $post_id
);
$notification = array(
// when application close then post field 'notification' parameter work
'body' => $content,
'sound' => 'default'
);
$post = array(
'condition' => $topic,
'notification' => $notification,
"content_available" => false,
'priority' => 'high',
'data' => $notification_data
);
//echo '<pre>';
//var_dump($post);
// Initialize curl handle
$ch = curl_init();
// Set URL to GCM endpoint
curl_setopt($ch, CURLOPT_URL, $url);
// Set request method to POST
curl_setopt($ch, CURLOPT_POST, true);
// Set our custom headers
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Get the response back as string instead of printing it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set JSON post data
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
// Actually send the push
$result = curl_exec($ch);
// Close curl handle
curl_close($ch);
// Debug GCM response
$result_de = json_decode($result);
return $result;
//var_dump($result); die;
}
}
$Firebase_Push_Notification_OBJ = new Firebase_Push_Notification();