ThinkPHP框架整合友盟推送DEMO

友盟是中国最大的移动开发者服务平台,为移动开发者提供免费的应用统计分析、社交分享、消息推送、自动更新、在线参数、移动推广效果分析、微社区等app开发和运营解决方案。

本博文讲述如何快速在ThinkPHP框架中集成友盟推送功能:

1、在官网或是在本博文内下载友盟推送PHP_DEMO;

2、将文件夹下的Notification文件夹放入到Application(应用文件目录)下;

3、在每个php文件内为文件根据文件夹的命名添加上合适的命名空间,笔者加的是:


//命名空间
namespace Notification;

4、在类文件Sms.class.php中将本人可以用到的函数进行重写完善,笔者就此稍举例子:
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());
		}
	}
	/**
	 * 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());
		}
	}
}
 
  
 
  

5、在自己的系统中进行调用:

$sms = new \Sms("5790414467e58ebc2f0008ae", "dgbtvr7myr3flllbpc6bww4gkfwjpnmv");
$sms->sendAndroidBroadcast("这是标题","这是内容");
$sms->sendAndroidUnicast("这是标题","这是内容",$tokens);
$sms->sendIOSBroadcast("这是标题","这是内容");
$sms->sendIOSUnicast("这是标题","这是内容",$tokens);


你可能感兴趣的:(ThinkPHP)