友盟是中國最大的移動開發者服務平臺,爲移動開發者提供免費的應用統計分析、社交分享、消息推送、自動更新、在線參數、移動推廣效果分析、微社區等app開發和運營解決方案。
本博文講述如何快速在ThinkPHP框架中集成友盟推送功能:
1、在官網或是在本博文內下載友盟推送PHP_DEMO;
2、將文件夾下的Notification文件夾放入到Application(應用文件目錄)下;
3、在每個php文件內爲文件根據文件夾的命名添加上合適的命名空間,筆者加的是:
-
//命名空間
-
namespace Notification;
4、在類文件Sms.class.php中將本人可以用到的函數進行重寫完善,筆者就此稍舉例子:
-
<?php
-
namespace Notification;
-
//引入核心文件
-
require_once('AndroidBroadcast.php');
-
require_once('AndroidFilecast.php');
-
require_once('AndroidGroupcast.php');
-
require_once('AndroidUnicast.php');
-
require_once('AndroidCustomizedcast.php');
-
require_once('IOSBroadcast.php');
-
require_once('IOSFilecast.php');
-
require_once('IOSGroupcast.php');
-
require_once('IOSUnicast.php');
-
require_once('IOSCustomizedcast.php');
-
class Sms {
-
protected $appkey = NULL;
-
protected $appMasterSecret = NULL;
-
protected $timestamp = NULL;
-
protected $validation_token = NULL;
-
function __construct($key, $secret) {
-
$this->appkey = $key;
-
$this->appMasterSecret = $secret;
-
$this->timestamp = strval(time());
-
}
-
/**
-
* Android推送—廣播
-
* @param $title string 推送消息標題
-
* @param $content string 推送消息內容
-
* @return mixed
-
*/
-
function sendAndroidBroadcast($title,$content) {
-
try {
-
$brocast = new AndroidBroadcast();
-
$brocast->setAppMasterSecret($this->appMasterSecret);
-
$brocast->setPredefinedKeyValue("appkey", $this->appkey);
-
$brocast->setPredefinedKeyValue("timestamp", $this->timestamp);
-
$brocast->setPredefinedKeyValue("ticker", "Android broadcast ticker");
-
$brocast->setPredefinedKeyValue("title", $title);
-
$brocast->setPredefinedKeyValue("text", $content);
-
$brocast->setPredefinedKeyValue("after_open", "go_app");
-
$brocast->setPredefinedKeyValue("production_mode", "true");
-
$brocast->setExtraField("test", "helloworld");
-
print("Sending broadcast notification, please wait...\r\n");
-
return $brocast->send();
-
print("Sent SUCCESS\r\n");
-
} catch (Exception $e) {
-
print("Caught exception: " . $e->getMessage());
-
}
-
}
-
/**
-
* Android推送—單播
-
* @param $title string 推送消息標題
-
* @param $content string 推送消息內容
-
* @param $tokens array 設備的token值
-
* @return mixed
-
*/
-
function sendAndroidUnicast($title,$content,$tokens) {
-
try {
-
$unicast = new AndroidUnicast();
-
$unicast->setAppMasterSecret($this->appMasterSecret);
-
$unicast->setPredefinedKeyValue("appkey", $this->appkey);
-
$unicast->setPredefinedKeyValue("timestamp", $this->timestamp);
-
$unicast->setPredefinedKeyValue("device_tokens", $tokens);
-
$unicast->setPredefinedKeyValue("ticker", "Android unicast ticker");
-
$unicast->setPredefinedKeyValue("title", $title);
-
$unicast->setPredefinedKeyValue("text", $content);
-
$unicast->setPredefinedKeyValue("after_open", "go_app");
-
$unicast->setPredefinedKeyValue("production_mode", "true");
-
$unicast->setExtraField("test", "helloworld");
-
print("Sending unicast notification, please wait...\r\n");
-
return $unicast->send();
-
print("Sent SUCCESS\r\n");
-
} catch (Exception $e) {
-
print("Caught exception: " . $e->getMessage());
-
}
-
}<pre name="code" class="php">
-
<span style="white-space:pre"> </span>/**
-
* IOS推送—廣播
-
* @param $title string 推送消息標題
-
* @param $content string 推送消息內容
-
* @return mixed
-
*/
-
function sendIOSBroadcast($title,$content) {
-
try {
-
$brocast = new IOSBroadcast();
-
$brocast->setAppMasterSecret($this->appMasterSecret);
-
$brocast->setPredefinedKeyValue("appkey", $this->appkey);
-
$brocast->setPredefinedKeyValue("timestamp", $this->timestamp);
-
$brocast->setPredefinedKeyValue("alert", $title);
-
$brocast->setPredefinedKeyValue("badge", 0);
-
$brocast->setPredefinedKeyValue("sound", "chime");
-
$brocast->setPredefinedKeyValue("production_mode", "false");
-
$brocast->setCustomizedField("test", $content);
-
print("Sending broadcast notification, please wait...\r\n");
-
return $brocast->send();
-
print("Sent SUCCESS\r\n");
-
} catch (Exception $e) {
-
print("Caught exception: " . $e->getMessage());
-
}
-
}
-
/**
-
* IOS推送—單播
-
* @param $title string 推送消息標題
-
* @param $content string 推送消息內容
-
* @param $tokens array 設備的token值
-
* @return mixed
-
*/
-
function sendIOSUnicast($title,$content,$tokens) {
-
try {
-
$unicast = new IOSUnicast();
-
$unicast->setAppMasterSecret($this->appMasterSecret);
-
$unicast->setPredefinedKeyValue("appkey", $this->appkey);
-
$unicast->setPredefinedKeyValue("timestamp", $this->timestamp);
-
$unicast->setPredefinedKeyValue("device_tokens", $tokens);
-
$unicast->setPredefinedKeyValue("alert", $title);
-
$unicast->setPredefinedKeyValue("badge", 0);
-
$unicast->setPredefinedKeyValue("sound", "chime");
-
$unicast->setPredefinedKeyValue("production_mode", "false");
-
$unicast->setCustomizedField("test", $content);
-
print("Sending unicast notification, please wait...\r\n");
-
return $unicast->send();
-
print("Sent SUCCESS\r\n");
-
} catch (Exception $e) {
-
print("Caught exception: " . $e->getMessage());
-
}
-
}<pre name="code" class="php">
-
}
5、在自己的系統中進行調用:
-
$sms = new \Sms("5790414467e58ebc2f0008ae", "dgbtvr7myr3flllbpc6bww4gkfwjpnmv");
-
$sms->sendAndroidBroadcast("這是標題","這是內容");
-
$sms->sendAndroidUnicast("這是標題","這是內容",$tokens);
-
$sms->sendIOSBroadcast("這是標題","這是內容");
-
$sms->sendIOSUnicast("這是標題","這是內容",$tokens);
轉載地址:https://blog.csdn.net/Zhihua_W/article/details/52250021?locationNum=12