-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.php
65 lines (56 loc) · 2.06 KB
/
functions.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
<?php
/*
Plugin Name: Android PUSH notifications
Plugin URI: https://gist.github.com/Victorivus/d0fc52464948c18b66fb2a54d77437a3
Description: send an Android PUSH notification when new post or page is published
Version: 1.0
Author: Victor N
Author URI: festicidas.com
*/
function send_notification($new_status, $old_status, $post){
if ( $new_status == 'publish' && ($old_status == 'draft' || $old_status == 'new' ||$old_status == 'pending' ||$old_status == 'auto-draft' ||$old_status == 'private') ) {
$post_id = $post->ID;
$post_title = $post->post_title;
$post_excerpt = $post->post_excerpt;
$url = 'https://fcm.googleapis.com/fcm/send';
###########################################
# YOUR API KEY GOES BELOW #
###########################################
$server_key = 'YOUR_API_KEY_GOES_HERE';
$notification = array();
$notification['body'] = $post_excerpt;
$notification['title'] = $post_title;
$notification['icon'] = "ic_launcher";
$data = array();
$data['id'] = $post_id;
$fields = array();
$fields['data'] = $data;
###########################################
# WHO YOU WANNA SEND THE NOTIFICATION TO #
###########################################
$fields['to'] = '/topics/news'; #Users subscribed to news
$fields['notification'] = $notification;
//header with content_type api key
$headers = array(
'Content-Type:application/json',
'Authorization:key='.$server_key
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
}
add_action( 'transition_post_status', 'send_notification', 10, 3 );
add_action( 'transition_page_status', 'send_notification', 10, 3 );
?>