duangao8359 2012-07-03 07:55
浏览 61
已采纳

iPhone中没有收到推送通知

I am a php developer. I am implementing a push notification module for iPhone. For that I am using php for server side implementation. Though I am getting "Connected to APNS {"aps":{"alert":"HI Push","badge":1,"sound":"default"}}Message successfully delivered" message, iPhone does not receive any notification. My php code is like:

<? 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); 
}

?>

objective c code is like:

 - (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条回答 默认 最新

  • 普通网友 2012-07-03 08:22
    关注

    Please check whether the certificates matching. For ex. If the iphone is in debug mode the server also use developer certificate for push notification and if the iPhone is build in the distribution, then the server use the production certificate for sending push notification. Please verify your certificates are matching. Also check the iPhone settings whether the notification is enabled for your app or not.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?