直接上代码
复制代码
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<?php use AppServicesJPushService; /** * 极光推送 * @param string $alias 别名 选填 最多一次推送一千个 限制字符长度40 * @param string $title android推送标题 必填 * @param string $alert ios推送标题 必填 * @param string $content 推送内容 必填 * @param string $extras 附加字段 选填 * @param string $builderId 通知栏样式 ID * @param string $type 推送类型 必填 全部所有:1(每天可发送十次) 根据标签:2 根据别名:3 根据目标(注册ID):4 * @param string $registrationId 注册ID 选填 最多一次推送一千个 * @param string $tag 标签 选填 最多一次推送20个 限制字符长度40 */ public Static function testJpush($alert='',$alias='',$title='',$content='',$extras='',$builderId='1',$type='',$registrationId='',$tag='') { // 推送平台 ios android $params['platform'] = 'all'; // android推送标题 $params['title'] = $title; // ios推送标题 $params['alert'] = $alert; // 推送内容 $params['content'] = $content; // 通知栏样式 ID $params['builderId'] = $builderId; // 附加字段(这里自定义 Key / value 信息,以供业务使用)$params['extras'] = ['orderid' => 13545]; $params['extras'] = $extras; // 别名 可以是单个 也可以是 数组$params['alias'] = ['51651545154','61654564897',]; $params['alias'] = $alias; // 标签 可以是单个 也可以是 数组$params['tag'] = ['51651545154','61654564897',]; $params['tag'] = $tag; // 注册ID 可以是单个 也可以是 数组$params['registrationId'] = ['170976fsdas554ewerr98f28','120c8545we15we46b8929e']; $params['registration_id'] = $registrationId; // 推送类型 全部所有:1 根据标签:2 根据别名:3 根据目标(注册ID):4 $params['type'] = $type; // var_dump($params); // die; //执行 $dat = JPushService::pushNotify($params); return $dat; // dump($dat);die; } /** * 获取指定设备的别名和标签 */ public static function getDevices($reg_id) { $response = JPushService::getInstance()->device()->getDevices($reg_id); if ($response['http_code'] == 200) { return $response['body']; } return []; } /** * 给指定设备添加标签 */ public static function addTags($reg_id, $tags = []) { $response = JPushService::getInstance()->device()->addTags($reg_id, $tags); if ($response['http_code'] == 200) { return true; } return false; } /** * 清空指定设备的标签 */ public static function clearTags($reg_id) { $response = JPushService::getInstance()->device()->clearTags($reg_id); if ($response['http_code'] == 200) { return true; } return false; } /** * 清空指定设备的标签 */ public static function removeTags($reg_id, $tags = []) { $response = JPushService::getInstance()->device()->removeTags($reg_id, $tags); if ($response['http_code'] == 200) { return true; } return false; } /** * 更新指定设备的别名 */ public static function updateAlias($reg_id, $alias) { $response = JPushService::getInstance()->device()->updateAlias($reg_id, $alias); if ($response['http_code'] == 200) { return true; } return false; }
复制代码
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<?php namespace AppServices; use JPushClient as JPush; use Log; class JPushService { protected static $client = null; //推送类型 //全部所有 const PUSH_TYPE_ALL = 1; //根据标签 const PUSH_TYPE_TAG = 2; //根据别名 const PUSH_TYPE_ALIAS = 3; //根据目标(注册ID) const PUSH_TYPE_REG_ID = 4; private function __construct() { } private function __clone() { } /** * 获取实例 */ public static function getInstance() { if (!self::$client) { self::$client = new JPush(config('app_key'), config('master_secret'), null); } return self::$client; } /** * 给android或ios推送消息 */ public static function pushNotify($params) { //推送平台 $platform = $params['platform'] ?? 'all'; //android推送标题 $title = $params['title'] ?? ''; //iOS推送标题 $alert = $params['alert'] ?? ''; //推送内容 $content = $params['content'] ?? ''; //通知栏样式ID $builder_id = $params['builder_id'] ?? 0; //附加字段 $extras = $params['extras'] ?? ''; //推送类型 $type = $params['type'] ?? ''; //推送目标(注册ID) $reg_id = $params['registration_id'] ?? ''; //推送目标(标签) $tag = $params['tag'] ?? ''; //推送目标(别名) $alias = $params['alias'] ?? ''; try { $push = self::getInstance()->push(); //设置平台 $push->setPlatform($platform); switch ($type) { case self::PUSH_TYPE_ALL: $push->addAllAudience(); break; case self::PUSH_TYPE_TAG: $push->addTag($tag); break; case self::PUSH_TYPE_ALIAS: $push->addAlias($alias); break; case self::PUSH_TYPE_REG_ID: $push->addRegistrationId($reg_id); break; } $push->androidNotification($content, [ 'title' => $title, 'builder_id' => $builder_id, 'extras' => $extras, ])->iosNotification(array( 'title' => $alert, //可选设置 // 'subtitle' => 'subtitle', //可选设置 'body' => $content//必填,否则通知栏不展示 ), [ 'sound' => 'sound', 'badge' => '+1', 'extras' => $extras ])->options([ 'apns_production' => config('jpush.apns_production', true), //表示离线消息保留时长(秒) 'time_to_live' => 86400, ]); $response = $push->send(); if ($response['http_code'] != 200) { Log::channel('jpush')->error(json_encode($response, JSON_UNESCAPED_UNICODE)); } return $response; } catch (Throwable $e) { Log::channel('jpush')->error(json_encode([ 'file' => $e->getFile(), 'line' => $e->getLine(), 'message' => $e->getMessage(), 'params' => $params, ], JSON_UNESCAPED_UNICODE)); } } /** * 获取指定设备的别名和标签 */ public static function getDevices($reg_id) { $response = self::getInstance()->device()->getDevices($reg_id); if ($response['http_code'] == 200) { return $response['body']; } return []; } /** * 给指定设备添加标签 */ public static function addTags($reg_id, $tags = []) { $response = self::getInstance()->device()->addTags($reg_id, $tags); if ($response['http_code'] == 200) { return true; } return false; } /** * 清空指定设备的标签 */ public static function clearTags($reg_id) { $response = self::getInstance()->device()->clearTags($reg_id); if ($response['http_code'] == 200) { return true; } return false; } /** * 清空指定设备的标签 */ public static function removeTags($reg_id, $tags = []) { $response = self::getInstance()->device()->removeTags($reg_id, $tags); if ($response['http_code'] == 200) { return true; } return false; } /** * 更新指定设备的别名 */ public static function updateAlias($reg_id, $alias) { $response = self::getInstance()->device()->updateAlias($reg_id, $alias); if ($response['http_code'] == 200) { return true; } return false; } }
最后
以上就是大胆犀牛最近收集整理的关于php实现极光推送的全部内容,更多相关php实现极光推送内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复