我正在尝试向在 iOS 8 上运行的应用程序发送推送通知。
我有两个设备;一台是运行 iOS 8 的 iPad 2,另一台是 iPhone 6+。
在两个设备上运行完全相同的应用程序,我使用相同的 php 脚本发送推送通知。
我正在使用以下 Objective C 代码来允许推送通知。
if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound
categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
// iOS < 8 Notifications
[application registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
以下是发送推送通知的 php 脚本
function sendpush($deviceToken, $message, $test = false){
// Put your private key's passphrase here:
$passphrase = '';
$pem = dirname(__FILE__) . '/ck-pushtest.pem';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $pem);
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client(
$test ? 'ssl://gateway.sandbox.push.apple.com:2195' : 'ssl://gateway.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Opening SSL Connection to Push Gateway Failed." . PHP_EOL);
// Create the payload body
$data['aps'] = array(
'content-available' => 1,
'alert' => $message,
'sound' => 'default',
'badge' => 1,
);
$data['push_type'] = 'link';
$data['link'] = 'http://google.com';
$payload = json_encode($data);
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
if ($result) echo '******************Success********************';
else '******************Failed*****************************';
// Close the connection to the server
fclose($fp);
}
// Put your device token here (without spaces):
$deviceToken = $_REQUEST['deviceToken'];
$message = 'Hi, this is a test message!';
$test = 1;
sendPush($deviceToken, $message, $test);
问题是在 iPad 2 (iOS 8) 上运行的应用程序会收到推送通知,但 iPhone 6+ 不会。
会出现什么问题呢?
任何建议将被认真考虑。