iPhone 未收到推送通知
Posted
技术标签:
【中文标题】iPhone 未收到推送通知【英文标题】:push-notification is not receiving in iPhone 【发布时间】:2012-07-03 07:55:43 【问题描述】:我是一名 php 开发人员。我正在为 iPhone 实现一个推送通知模块。为此,我使用 php 进行服务器端实现。虽然我收到“已连接到 APNS "aps":"alert":"HI Push","badge":1,"sound":"default"消息已成功传递”消息,但 iPhone 没有收到任何通知.我的php代码是这样的:
<? php
include('include/connect.php');
$device = mysql_query("SELECT device_token,badge,alert,sound FROM push_notification WHERE device_status='1' ");
while($res = mysql_fetch_array($device))
// Put your device token here (without spaces):
$deviceToken = $res['device_token'];
// Put your private key's passphrase here:
$passphrase = "pushchat";
// Put your alert message here:
//$message = trim($_REQUEST['alert']);
$message = "HI Push" ;
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.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;
$body['aps'] = array('alert' => $message,'badge' => 1,'sound' => 'default'); // Encode the payload as JSON
$payload = json_encode($body);
echo $payload;
// 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);
?>
目标c代码如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
// Register for Push Notification Type
deviceTokenString=[[NSString alloc]init];
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound) ];
/*---------------Increasing Badge No-----------*/
application.applicationIconBadgeNumber=0;
#pragma mark-
#pragma mark PushNotification Delegate methods
/*------ Provide a user explanation for a place to get Device Token-------*/
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
/*---------Get The Device Token here--------------*/
deviceTokenString = [deviceToken description];
deviceTokenString = [deviceTokenString stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
deviceTokenString = [deviceTokenString stringByReplacingOccurrencesOfString:@" " withString:@""];
/*------ Provide a user explanation for when the registration fails-------*/
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
NSLog(@"Error in registration. Error: %@", error);
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
NSLog(@"Received Notification");
NSLog(@"remote notification: %@",[userInfo description]);
NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
NSString *alert = [apsInfo objectForKey:@"alert"];
NSLog(@"Received Push Alert: %@", alert);
NSString *sound = [apsInfo objectForKey:@"sound"];
NSLog(@"Received Push Sound: %@", sound);
NSString *badge = [apsInfo objectForKey:@"badge"];
NSLog(@"Received Push Badge: %@", badge);
application.applicationIconBadgeNumber = [[apsInfo objectForKey:@"badge"] integerValue];
【问题讨论】:
您是否拥有正确的证书组合——沙盒服务器使用测试证书和开发者证书来构建应用程序? 【参考方案1】:请检查证书是否匹配。例如。如果 iphone 处于调试模式,服务器也使用开发者证书来推送通知,如果 iPhone 是在发行版中构建的,那么服务器使用生产证书来发送推送通知。请验证您的证书是否匹配。还要检查 iPhone 设置是否为您的应用启用了通知。
【讨论】:
非常感谢,开发者证书可以正常使用。 但在生产证书中,我无法获取设备令牌。我们的服务器有什么需要改变的吗?请帮帮我。 在使用生产证书时,您不能使用沙箱发送推送通知。必须使用直接 apns 服务器 url 发送推送通知。 点赞$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
很抱歉,我没有注意到您无法获取设备令牌。为此,请尝试从您的系统中删除现有的分发证书。从您的 Apple 开发者帐户中重新下载它,然后尝试使用该证书进行构建。以上是关于iPhone 未收到推送通知的主要内容,如果未能解决你的问题,请参考以下文章