我想通过pushwoosh从客户端(我的cordova(ionic)应用程序)发送推送通知。
有什么样的代码或指南吗?
谢谢!
您必须使用远程访问API,这需要付费计划。我创建了一个对象,其中包含我想要发送的文本和我想要发送到的设备令牌,并将其作为参数传递给以下函数:
function push(object) {
var params = {
"request": {
"application": "PW_ID GOES HERE",
"auth": "Find in your API Access page",
"notifications": [{
// Content Settings
"send_date": "now",
"ignore_user_timezone": true,
"content": {
"en": object.text
},
"platforms": [1, 3], // 1 - iOS; 3 - Android;
// iOS Related
"ios_category_id": "1",
"ios_badges": "+1",
// Android Related
"android_icon": "icon",
// Who to send it to
"devices": object.tokens
}]
}
};
$http.post('https://cp.pushwoosh.com/json/1.3/createMessage', params).then(success, failure);
function success() {
console.log("successful notification push");
}
function failure(error) {
console.log('error sending notification', error);
}
}
看看Phonegap的插件https://github.com/Pushwoosh/pushwoosh-phonegap-3.0-plugin和Pushwoosh网站上的文档https://www.pushwoosh.com/programming-push-notification/ios/ios-additional-platforms/push-notification-sdk-integration-for-phonegap/.
这里有一些类似于我使用的内容
- 向服务器发送请求
$http.get('http://test.com/push.php?token=' + ushToken + '&message=' + encodeURIComponent(message)).then(function (resp) {
}, function (err) {
console.error(err);
});
- 在服务器端do
<?php
define('PW_AUTH', '...');
define('PW_APPLICATION', '...');
define('PW_DEBUG', FALSE);
function pwCall($method, $data)
{
$url = 'https://cp.pushwoosh.com/json/1.3/' . $method;
$request = json_encode(['request' => $data]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if (defined('PW_DEBUG') && PW_DEBUG) {
var_dump(json_decode($request));
var_dump($response);
var_dump($info);
}
}
if (isset($_GET['message']) && isset($_GET['token'])) {
$payload = [
'application' => PW_APPLICATION,
'auth' => PW_AUTH,
'notifications' => [
[
'send_date' => 'now',
'content' => $_GET['message'],
'devices' => [$_GET['token']]
]
]
];
pwCall('createMessage', $payload);
}