Home > Backend Development > PHP Tutorial > PHP Umeng message push class

PHP Umeng message push class

不言
Release: 2023-03-28 19:06:01
Original
5489 people have browsed it

This article mainly introduces the PHP Umeng message push category, which has a certain reference value. Now I share it with everyone. Friends in need can refer to it

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

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

<?php

/**

 * 友盟消息 安卓 IOS 推送类

 *

 */

 

class AlliesClass

{

    protected $host; //发送地址

    protected $app_key; //appkey

    protected $appMasterSecret; //app secret

    protected $description; //app secret

 

    //

    public function __construct($options = null)

    {

        if (is_array($options)){

 

            $this->host = &#39;https://msgapi.umeng.com/api/send&#39;;

            $this->app_key = $options[&#39;app_key&#39;];

            $this->appMasterSecret = $options[&#39;appMasterSecret&#39;];

 

            $this->description = "友盟接口推送";

 

        }else{

            return false;

        }

 

    }

 

 

    /**

     * @param $info

     * @param $device_token

     * @return mixed|string

     * 用户单播 和 列播

     */

    public function Android_Device_Push($info, $device_token)

    {

        $data[&#39;appkey&#39;] = $this->app_key;

        $data[&#39;timestamp&#39;] = time(); //时间戳

 

        if(is_array($device_token)){

 

            //批量用户列播

            $data[&#39;type&#39;] = &#39;listcast&#39;;

            $data[&#39;device_tokens&#39;] =  implode(&#39;,&#39;,$device_token); //数组转字符串

 

        }else{

 

            //一个用户单播

            $data[&#39;type&#39;] = &#39;unicast&#39;;

            $data[&#39;device_tokens&#39;] =  $device_token;

        }

 

        //payload内容

        $data[&#39;payload&#39;][&#39;display_type&#39;] = &#39;notification&#39;; //通知消息

 

        //payload body内容

        $data[&#39;payload&#39;][&#39;body&#39;][&#39;after_open&#39;] = "go_custom"; //后续操作打开app

 

        $data[&#39;payload&#39;][&#39;body&#39;][&#39;ticker&#39;] = $info[&#39;ticker&#39;];

        $data[&#39;payload&#39;][&#39;body&#39;][&#39;title&#39;] = $info[&#39;title&#39;];

        $data[&#39;payload&#39;][&#39;body&#39;][&#39;text&#39;] = $info[&#39;title&#39;]; //广播通知不能为空补填

 

        //这里可以写附加字段

        $data[&#39;payload&#39;][&#39;extra&#39;][&#39;type&#39;] = $info[&#39;type&#39;];  //附加字段类型

 

 

        $data[&#39;production_mode&#39;] = $info[&#39;production_mode&#39;];

 

        $data[&#39;description&#39;] = $this->description;

 

        return $this->send($data, $this->host, $this->appMasterSecret);

    }

 

    /**

     * @param $info

     * @return mixed|string

     * 广播

     */

    public function Android_Broadcast($info)

    {

        $data[&#39;appkey&#39;] = $this->app_key;

        $data[&#39;timestamp&#39;] = time(); //时间戳

 

        //广播消息

        $data[&#39;type&#39;] = &#39;broadcast&#39;;

 

        //payload内容

        $data[&#39;payload&#39;][&#39;display_type&#39;] = &#39;notification&#39;; //通知消息

 

        //payload body内容

        $data[&#39;payload&#39;][&#39;body&#39;][&#39;after_open&#39;] = "go_custom"; //后续操作打开app

 

        $data[&#39;payload&#39;][&#39;body&#39;][&#39;ticker&#39;] = $info[&#39;ticker&#39;];

        $data[&#39;payload&#39;][&#39;body&#39;][&#39;title&#39;] = $info[&#39;title&#39;];

        $data[&#39;payload&#39;][&#39;body&#39;][&#39;text&#39;] = $info[&#39;title&#39;]; //广播通知不能为空补填

 

        $data[&#39;payload&#39;][&#39;extra&#39;][&#39;type&#39;] = $info[&#39;type&#39;];  //附加字段类型1 跳转消息详情

        $data[&#39;payload&#39;][&#39;extra&#39;][&#39;prod_id&#39;] = $info[&#39;prod_id&#39;];  //附加字段消息详情id

        $data[&#39;payload&#39;][&#39;extra&#39;][&#39;text&#39;] = $info[&#39;text&#39;]; //

 

        $data[&#39;production_mode&#39;] = $info[&#39;production_mode&#39;];

 

        $data[&#39;description&#39;] = $this->description;

 

        return $this->send($data, $this->host, $this->appMasterSecret);

    }

 

    /**

     * @param $info

     * @param $device_token

     * @return mixed|string

     * 单播 和 列播

     */

    public function Ios_Device_Push($info, $device_token)

    {

        $data = array();

 

        $data[&#39;appkey&#39;] = $this->app_key;

        $data[&#39;timestamp&#39;] = time(); //时间戳

 

        if(is_array($device_token)){

 

            //批量用户列播

            $data[&#39;type&#39;] = &#39;listcast&#39;;

            $data[&#39;device_tokens&#39;] =  implode(&#39;,&#39;,$device_token); //数组转字符串

 

        }else{

 

            //一个用户单播

            $data[&#39;type&#39;] = &#39;unicast&#39;;

            $data[&#39;device_tokens&#39;] =  $device_token;

        }

 

        //payload内容

        $data[&#39;payload&#39;][&#39;aps&#39;][&#39;alert&#39;] = $info[&#39;text&#39;]; //消息主体

        $data[&#39;payload&#39;][&#39;aps&#39;][&#39;sound&#39;] = &#39;default&#39;; //声音

 

        $data[&#39;payload&#39;][&#39;type&#39;] = $info[&#39;type&#39;]; //消息类型 0打开消息详情

 

        $data[&#39;payload&#39;][&#39;prod_id&#39;] = $info[&#39;prod_id&#39;]; //消息id

        $data[&#39;payload&#39;][&#39;title&#39;] = $info[&#39;title&#39;];

        $data[&#39;payload&#39;][&#39;text&#39;] = $info[&#39;text&#39;]; //

 

        $data[&#39;production_mode&#39;] = $info[&#39;production_mode&#39;];

 

        $data[&#39;description&#39;] = $this->description;

 

        return $this->send($data, $this->host, $this->appMasterSecret);

    }

 

 

    public function Ios_Broadcast($info)

    {

        $data = array();

 

        $data[&#39;appkey&#39;] = $this->app_key;

        $data[&#39;timestamp&#39;] = time(); //时间戳

 

        //广播消息

        $data[&#39;type&#39;] = &#39;broadcast&#39;;

 

        //payload内容

        $data[&#39;payload&#39;][&#39;aps&#39;][&#39;alert&#39;] = $info[&#39;title&#39;]; //消息主体

        $data[&#39;payload&#39;][&#39;aps&#39;][&#39;sound&#39;] = &#39;chime&#39;; //声音

        $data[&#39;payload&#39;][&#39;aps&#39;][&#39;badge&#39;] = 1; //显示角标

 

        $data[&#39;payload&#39;][&#39;type&#39;] = $info[&#39;type&#39;]; //消息类型 0打开消息详情

 

        $data[&#39;payload&#39;][&#39;prod_id&#39;] = $info[&#39;prod_id&#39;]; //消息id

        $data[&#39;payload&#39;][&#39;title&#39;] = $info[&#39;title&#39;];

        $data[&#39;payload&#39;][&#39;text&#39;] = $info[&#39;ticker&#39;]; //

 

        $data[&#39;production_mode&#39;] = $info[&#39;production_mode&#39;];

 

        $data[&#39;description&#39;] = $this->description;

 

        return $this->send($data, $this->host, $this->appMasterSecret);

    }

 

    /**

     * @param $data

     * @param $url_s

     * @param $appMasterSecret

     * @return mixed|string

     * curl 请求

     */

    private function send($data, $url_s, $appMasterSecret)

    {

        $postBody = json_encode($data);

 

        //加密

        $sign = md5("POST" . $url_s . $postBody . $appMasterSecret);

        $url = $url_s . "?sign=" . $sign;

 

        $ch = curl_init($url);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

        curl_setopt($ch, CURLOPT_POST, 1);

        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

        curl_setopt($ch, CURLOPT_TIMEOUT, 5);

        curl_setopt($ch, CURLOPT_POSTFIELDS, $postBody );

        $result = curl_exec($ch);

        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        $curlErrNo = curl_errno($ch);

        $curlErr = curl_error($ch);

        curl_close($ch);

 

        // print_r($result);

        // exit;

 

        if ($httpCode == "0") {

            // Time out

            return ("Curl error number:" . $curlErrNo . " , Curl error details:" . $curlErr . "\r\n");

        } else if ($httpCode != "200") {

            return ("Http code:" . $httpCode " details:" . $result . "\r\n");

        } else {

            return $result;

        }

    }

}

 

?>

Copy after login

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<?php

    //使用类

    $options[&#39;app_key&#39;] = &#39;&#39;; //友盟key;

    $options[&#39;appMasterSecret&#39;] = &#39;&#39;; //友盟MasterSecret

 

    $this->umeng = new AlliesClass($options);

 

    $info[&#39;ticker&#39;] = &#39;&#39;;

    $info[&#39;title&#39;] =&#39;&#39;;

    $info[&#39;title&#39;] =&#39;&#39;; //广播通知不能为空补填

    $info[&#39;type&#39;] = 4;  //附加字段类型1 跳转消息详情

    $info[&#39;text&#39;] = &#39;提醒&#39;; //

    $info[&#39;production_mode&#39;] = &#39;false&#39;;

 

    $value=$this->umeng->Android_Device_Push($info, $token);  //单播传入数据和用户token

 

    $value=$this->umeng->Android_Broadcast($info);  //广播直接传入数据

?>

Copy after login

The above is the entire content of this article Well, thank you all for reading. Please pay attention to the PHP Chinese website for more information!

Related recommendations:

PHP mortgage calculation

PHP screening method to find prime numbers

The above is the detailed content of PHP Umeng message push class. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template