简单粗暴直接上代码
这个是极光插件js代码调用
//初始化极光插件
window.plugins.jPushPlugin.init();
//获取registrationID
var onGetRegistrationID = function(data) {
try {
if (data.length == 0) {
var t1 = window.setTimeout(getRegistrationID, 1000);
} else {
localStorage["rid"] = data;
}
} catch (exception) {
console.log(exception);
}
};
var getRegistrationID = function() {
window.plugins.jPushPlugin.getRegistrationID(onGetRegistrationID);
};
getRegistrationID();
//以下是获取消息后的回掉
var onReceiveNotification = function(event) {};
var onOpenNotification = function(event) {};
var onBackgroundNotification = function(event) {};
//Android 收到应用内消息时
document.addEventListener("jpush.receiveNotification", onReceiveNotification, false);
//点击 Android 手机的通知栏进入应用程序时
document.addEventListener("jpush.openNotification", onOpenNotification, false);
//最新版本已经没这个方法了,记得好像是程序进入后台,将会执行的
document.addEventListener("jpush.backgroundNotification", onBackgroundNotification, false);
php 接口使用
注:
1、regid 就是上文获取的registrationID, message则是要发的消息
2、JPush是极光提供的类文件,只需要加载Jgpush/autoload.php这个文件即可
下载地址:http://docs.jiguang.cn/jpush/server/3rd/php_sdk/
(下载的sdk可能和我下载的版本不同,所以具体如何调用,还需查看相关资料)
$app_key = self::APPKEY; //填入你的极光app_key
$master_secret = self::MASTER_SECRET; //填入你的极光master_secret
$client = new JPush($app_key,$master_secret);
$result = $client->push()->setPlatform('all')->addRegistrationId($regid)
->iosNotification($message, [
'sound' => '铃声.caf', //iOS自定义铃声
'badge' => '1',
'ontent-available'=>true,
'extras' => [
'key' => $app_key
]
])->androidNotification($message, [
"alert"=>$message,
'extras' => [
'key' => $app_key
]
])->options(array('apns_production' => true))->send();
return $result; //这里返回的是极光推送成功与否的反馈
网友评论