iOS , Phone gap push plugin on('notification') 未触发
iOS , Phone gap push plugin on('notification') not triggered
我正在使用 (push-plugin) 通过 phone Gap 向 iPhone 发送通知。
What I have done
- 为 phoneGap 项目添加了推送插件
- 我正在成功获取设备令牌。
- 发送成功
使用服务器 (PHP) 作为后端
向 APN 发送通知
- 但是我收不到通知
Here is my working code(phoneGap - client side):
var push = PushNotification.init({
ios: {
"alert": "true",
"badge": "true",
"sound": 'false'
}
});
push.on('registration', function(data) {
console.log('your device token',data.registrationId);
});
push.on('notification', function(data) {
console.log(('test');
console.log(data.message);
console.log(data.title);
console.log(data.count);
console.log(data.sound);
console.log(data.image);
console.log(data.additionalData);
});
push.on('error', function(e) {
console.log(e.message);
});
Here is my Server code (PHP)
<?php
$deviceToken = '<my _device_token>';
// Put your private key's passphrase here:
$passphrase = '<pass_phrase>';
// Put your alert message here:
$message = "This is test notification";
$url = "http://google.com";
if (!$message || !$url)
exit('Example Usage: $php newspush.php \'Breaking News!\' \'https://google.com\'' . "\n");
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'abcdxyz.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default',
'link_url' => $url,
'category' => "NEWS_CATEGORY",
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
ENVIRONMENT:
corodova version : 6.1.2
phonegap-plugin-push version : 1.6.2
iPhone version : 9.2
QUSETION Why my mobile device unable to receive notifications?
我通过在 push.on('notification',function(){});
中添加以下函数解决了上述问题
push.finish()
示例:
push.on('notification', function(data)
{
console.log(data.message);
console.log(data.title);
push.finish(function ()
{
console.log('finish successfully called');
});
});
我正在使用 (push-plugin) 通过 phone Gap 向 iPhone 发送通知。
What I have done
- 为 phoneGap 项目添加了推送插件
- 我正在成功获取设备令牌。
- 发送成功 使用服务器 (PHP) 作为后端 向 APN 发送通知
- 但是我收不到通知
Here is my working code(phoneGap - client side):
var push = PushNotification.init({
ios: {
"alert": "true",
"badge": "true",
"sound": 'false'
}
});
push.on('registration', function(data) {
console.log('your device token',data.registrationId);
});
push.on('notification', function(data) {
console.log(('test');
console.log(data.message);
console.log(data.title);
console.log(data.count);
console.log(data.sound);
console.log(data.image);
console.log(data.additionalData);
});
push.on('error', function(e) {
console.log(e.message);
});
Here is my Server code (PHP)
<?php
$deviceToken = '<my _device_token>';
// Put your private key's passphrase here:
$passphrase = '<pass_phrase>';
// Put your alert message here:
$message = "This is test notification";
$url = "http://google.com";
if (!$message || !$url)
exit('Example Usage: $php newspush.php \'Breaking News!\' \'https://google.com\'' . "\n");
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'abcdxyz.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default',
'link_url' => $url,
'category' => "NEWS_CATEGORY",
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
ENVIRONMENT: corodova version : 6.1.2 phonegap-plugin-push version : 1.6.2 iPhone version : 9.2
QUSETION Why my mobile device unable to receive notifications?
我通过在 push.on('notification',function(){});
push.finish()
示例:
push.on('notification', function(data)
{
console.log(data.message);
console.log(data.title);
push.finish(function ()
{
console.log('finish successfully called');
});
});