前言:
手机应用上经常需要用到推送服务,比如向所有设备推送一条消息,或者在自己的服务器上向单个设备推送消息,这是一个给力的教程。
Unity3D(客户端)
先在极光推送官网上下载Unity3D的插件:
https://github.com/jpush/jpush-unity3d-plugin
将插件导入到你的工程里。
按照教学替换AndroidManifest.xml里的包名,然后在代码里加一行代码
JPushBinding.initJPush ();
//开启极光推送服务
这样在客户端里就可以用推送了,你也可以设置别名让这个客户端有唯一标识
JPushBinding.setAlias (PlayerPrefs.GetString
("account"));
上面我用了自己的账号作为别名。
在官网就可以上所有注册的设备发送消息,方便吧!
PHP(服务器)
若是想要从自己的服务器上向设备发送通知,先去官网下载依赖包
http://docs.jpush.io/server/php_sdk/
将这个依赖包放在PHP文件的目录下,然后在PHP开头加上:
require_once 'vendor/autoload.php';
use JPush\Model as M;
use JPush\JPushClient;
use JPush\Exception\APIConnectionException;
use JPush\Exception\APIRequestException;
然后输入你在官网上申请应用自动生成的:
$app_key = "5e35f1d6c0acff55560a5fd5";
$master_secret = "a9745b7761a36c3334ed36b8";
$client = new JPushClient($app_key, $master_secret);
然后就可以根据你客户端的别名,向指定的设备推送一个消息:
$result = $client->push()
->setPlatform(M\all)
->setAudience(M\alias(array($receiver)))
->setNotification(M\notification($content))
->send();
以上$receiver为客户端别名,$content为你要发送的信息
想要更多参数的用法,可以去官方看文档。