php - iPhone 收不到推送通知

标签 php iphone push-notification

我是一名 PHP 开发人员。我正在为 iPhone 实现一个推送通知模块。为此,我将 php 用于服务器端实现。虽然我收到“连接到 APNS {"aps":{"alert":"HI Push","badge":1,"sound":"default"}}Message successful delivered"消息,但 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); 
}

?>

objective-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];


}

最佳答案

请检查证书是否匹配。对于前。如果 iPhone 处于 Debug模式,服务器也使用开发者证书来推送通知,如果 iPhone 是在发行版中构建的,则服务器使用生产证书来发送推送通知。请验证您的证书是否匹配。还要检查 iPhone 设置是否为您的应用启用了通知。

关于php - iPhone 收不到推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11306678/

相关文章:

ios - 实现服务器

android - 如何更新 Android 中的现有通知?

php - 如何检测数据库结构是否发生变化? (不是内容!)

php - 如何使用 foreach 循环访问二维数组的第一级键?

javascript - 如何从 mysql 检索数据到 chart.js

iOS 7 MPVolumeView 改变大小

php - 获取php中字符串的中间部分

ios - 如何使用自定义操作关闭 3D 触摸通知?

iPhone 模拟器卡住整个操作系统

node.js - 如何在没有 Firebase 和自己的自定义后端的情况下使用 Flutter 设置推送通知