Phonegap iOS 未收到推送通知
Phonegap iOS push notifications not received
我们管理两个平台 iOS 和 Android 的推送通知。 Android 的程序很好,设备已注册到 GCM 并收到通知。问题是 iOS 的 APNS,没有收到通知!即使设备已正确注册并且 APNS 也为设备生成了令牌。
下面是接收推送的Javascript和发送消息的PHP代码。
Javascript接收推送代码:
var pushNotification;
document.addEventListener("deviceready", onDeviceReadyEvent, false);
function onDeviceReadyEvent(){
pushNotification = window.plugins.pushNotification;
var sk_deviceplatform = device.platform;
sk_deviceplatform = sk_deviceplatform.toLowerCase();
if(sk_deviceplatform === 'android'){
pushNotification.register(successHandler, errorHandler, {"senderID":"XXXXXXXXX","ecb":"onNotificationGCM"});
} else {
pushNotification.register(tokenHandler, errorHandler, {"badge":"true","sound":"true","alert":"true","ecb":"onNotificationAPN"});
}
}
function tokenHandler(result) {
console.log("Token: " + result);
alert("Token: "+ result);
}
function errorHandler(error) {
console.log("Error: " + error);
alert('Error:' + error);
}
function onNotificationAPNS(e){
if(e.alert.title) {
$.mobile.changePage( "handle_notifications.html?id="+e.eventid, { transition: "slide"} );
}
if(e.sound) {
var skpn_snd = new Media(e.sound);
skpn_snd.play();
}
if (e.badge) {
pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, e.badge);
}
if (e.foreground===0){
// when application is not active
}else{
navigator.notification.alert(e.alert.title, null, 'News Notification', 'OK');
}
}
PHP发送推送代码:
/*** PUSH NOTIFICATION FOR IOS VIA APNS ***/
set_time_limit(0);
// charset header for output
header('content-type: text/html; charset: utf-8');
$deviceIds = array(/* get all devices token ids from the database */);
if(count($deviceIds)>0){
// this is where you can customize your notification
$body['aps'] = array(
'badge' => +1,
'alert' => "News Event!",
'sound' => 'default'
);
$payload = json_encode($body);
////////////////////////////////////////////////////////////////////////////////
// start to create connection
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', "XXXX.pem");
stream_context_set_option($ctx, 'ssl', 'passphrase', "XXXXXXX");
foreach ($deviceIds as $item_device) {
// Open a connection to the APNS server
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
if(!$fp){ exit("Failed to connect: $err $errstr" . '<br />');}else{/* service online */}
// Build the binary notification
$msg_notification = chr(0) . pack('n', 32) . pack('H*', $item_device) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg_notification, strlen($msg_notification));
if (!$result) { echo 'Undelivered message count: ' . $item_device . '<br />';}
else { /* notifications are sent */ }
if ($fp){
## check for errors
$apple_error_response = fread($fp, 6); //byte1=always 8, byte2=StatusCode, bytes3,4,5,6=identifier(rowID). Should return nothing if OK.
//NOTE: Make sure you set stream_set_blocking($fp, 0) or else fread will pause your script and wait forever when there is no response to be sent.
if ($apple_error_response) {
$error_response = unpack('Ccommand/Cstatus_code/Nidentifier', $apple_error_response); //unpack the error response (first byte 'command" should always be 8)
if ($error_response['status_code'] == '0') {
$error_response['status_code'] = '0-No errors encountered';
} else if ($error_response['status_code'] == '1') {
$error_response['status_code'] = '1-Processing error';
} else if ($error_response['status_code'] == '2') {
$error_response['status_code'] = '2-Missing device token';
} else if ($error_response['status_code'] == '3') {
$error_response['status_code'] = '3-Missing topic';
} else if ($error_response['status_code'] == '4') {
$error_response['status_code'] = '4-Missing payload';
} else if ($error_response['status_code'] == '5') {
$error_response['status_code'] = '5-Invalid token size';
} else if ($error_response['status_code'] == '6') {
$error_response['status_code'] = '6-Invalid topic size';
} else if ($error_response['status_code'] == '7') {
$error_response['status_code'] = '7-Invalid payload size';
} else if ($error_response['status_code'] == '8') {
$error_response['status_code'] = '8-Invalid token';
} else if ($error_response['status_code'] == '255') {
$error_response['status_code'] = '255-None (unknown)';
} else {
$error_response['status_code'] = $error_response['status_code'].'-Not listed';
}
echo '<br><b>ERROR</b> Response Command:<b>' . $error_response['command'] . '</b> Identifier:<b>' . $error_response['identifier'] . '</b> Status:<b>' . $error_response['status_code'] . '</b><br>';
echo 'Identifier is the rowID (index) in the database that caused the problem, and Apple will disconnect you from server. To continue sending Push Notifications, just start at the next rowID after this Identifier.<br>';
}
fclose($fp);
$_ENV['connection_status'] = 'The connection has been closed by the client' . '<br />';
}
}
set_time_limit(30);
}
当我们发送消息时,没有错误,一切都很好,但是没有收到推送通知。
问题要么在 PHP 脚本内部,要么在 apache cordova 脚本中...
感谢您的建议...
您忘记安装这两个 cordova 插件了。
https://github.com/apache/cordova-plugin-device
https://github.com/apache/cordova-plugin-console
因为这两个,你无法检测到你的 device type
和 console
你的输出。
我们发现了问题...我们尝试使用生产连接服务器通过带有开发证书的 APNS 发送推送。
对于生产,请使用以下连接:
ssl://gateway.push.apple.com:2195
如需开发,请使用以下连接:
ssl://gateway.sandbox.push.apple.com:2195
我们管理两个平台 iOS 和 Android 的推送通知。 Android 的程序很好,设备已注册到 GCM 并收到通知。问题是 iOS 的 APNS,没有收到通知!即使设备已正确注册并且 APNS 也为设备生成了令牌。
下面是接收推送的Javascript和发送消息的PHP代码。
Javascript接收推送代码:
var pushNotification;
document.addEventListener("deviceready", onDeviceReadyEvent, false);
function onDeviceReadyEvent(){
pushNotification = window.plugins.pushNotification;
var sk_deviceplatform = device.platform;
sk_deviceplatform = sk_deviceplatform.toLowerCase();
if(sk_deviceplatform === 'android'){
pushNotification.register(successHandler, errorHandler, {"senderID":"XXXXXXXXX","ecb":"onNotificationGCM"});
} else {
pushNotification.register(tokenHandler, errorHandler, {"badge":"true","sound":"true","alert":"true","ecb":"onNotificationAPN"});
}
}
function tokenHandler(result) {
console.log("Token: " + result);
alert("Token: "+ result);
}
function errorHandler(error) {
console.log("Error: " + error);
alert('Error:' + error);
}
function onNotificationAPNS(e){
if(e.alert.title) {
$.mobile.changePage( "handle_notifications.html?id="+e.eventid, { transition: "slide"} );
}
if(e.sound) {
var skpn_snd = new Media(e.sound);
skpn_snd.play();
}
if (e.badge) {
pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, e.badge);
}
if (e.foreground===0){
// when application is not active
}else{
navigator.notification.alert(e.alert.title, null, 'News Notification', 'OK');
}
}
PHP发送推送代码:
/*** PUSH NOTIFICATION FOR IOS VIA APNS ***/
set_time_limit(0);
// charset header for output
header('content-type: text/html; charset: utf-8');
$deviceIds = array(/* get all devices token ids from the database */);
if(count($deviceIds)>0){
// this is where you can customize your notification
$body['aps'] = array(
'badge' => +1,
'alert' => "News Event!",
'sound' => 'default'
);
$payload = json_encode($body);
////////////////////////////////////////////////////////////////////////////////
// start to create connection
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', "XXXX.pem");
stream_context_set_option($ctx, 'ssl', 'passphrase', "XXXXXXX");
foreach ($deviceIds as $item_device) {
// Open a connection to the APNS server
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
if(!$fp){ exit("Failed to connect: $err $errstr" . '<br />');}else{/* service online */}
// Build the binary notification
$msg_notification = chr(0) . pack('n', 32) . pack('H*', $item_device) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg_notification, strlen($msg_notification));
if (!$result) { echo 'Undelivered message count: ' . $item_device . '<br />';}
else { /* notifications are sent */ }
if ($fp){
## check for errors
$apple_error_response = fread($fp, 6); //byte1=always 8, byte2=StatusCode, bytes3,4,5,6=identifier(rowID). Should return nothing if OK.
//NOTE: Make sure you set stream_set_blocking($fp, 0) or else fread will pause your script and wait forever when there is no response to be sent.
if ($apple_error_response) {
$error_response = unpack('Ccommand/Cstatus_code/Nidentifier', $apple_error_response); //unpack the error response (first byte 'command" should always be 8)
if ($error_response['status_code'] == '0') {
$error_response['status_code'] = '0-No errors encountered';
} else if ($error_response['status_code'] == '1') {
$error_response['status_code'] = '1-Processing error';
} else if ($error_response['status_code'] == '2') {
$error_response['status_code'] = '2-Missing device token';
} else if ($error_response['status_code'] == '3') {
$error_response['status_code'] = '3-Missing topic';
} else if ($error_response['status_code'] == '4') {
$error_response['status_code'] = '4-Missing payload';
} else if ($error_response['status_code'] == '5') {
$error_response['status_code'] = '5-Invalid token size';
} else if ($error_response['status_code'] == '6') {
$error_response['status_code'] = '6-Invalid topic size';
} else if ($error_response['status_code'] == '7') {
$error_response['status_code'] = '7-Invalid payload size';
} else if ($error_response['status_code'] == '8') {
$error_response['status_code'] = '8-Invalid token';
} else if ($error_response['status_code'] == '255') {
$error_response['status_code'] = '255-None (unknown)';
} else {
$error_response['status_code'] = $error_response['status_code'].'-Not listed';
}
echo '<br><b>ERROR</b> Response Command:<b>' . $error_response['command'] . '</b> Identifier:<b>' . $error_response['identifier'] . '</b> Status:<b>' . $error_response['status_code'] . '</b><br>';
echo 'Identifier is the rowID (index) in the database that caused the problem, and Apple will disconnect you from server. To continue sending Push Notifications, just start at the next rowID after this Identifier.<br>';
}
fclose($fp);
$_ENV['connection_status'] = 'The connection has been closed by the client' . '<br />';
}
}
set_time_limit(30);
}
当我们发送消息时,没有错误,一切都很好,但是没有收到推送通知。 问题要么在 PHP 脚本内部,要么在 apache cordova 脚本中...
感谢您的建议...
您忘记安装这两个 cordova 插件了。
https://github.com/apache/cordova-plugin-device
https://github.com/apache/cordova-plugin-console
因为这两个,你无法检测到你的 device type
和 console
你的输出。
我们发现了问题...我们尝试使用生产连接服务器通过带有开发证书的 APNS 发送推送。 对于生产,请使用以下连接:
ssl://gateway.push.apple.com:2195
如需开发,请使用以下连接:
ssl://gateway.sandbox.push.apple.com:2195