当前位置: 首页 > 知识库问答 >
问题:

在苹果手机中未收到推送通知

白越
2023-03-14

我是一名php开发人员。我正在为iPhone实现推送通知模块。为此,我使用php进行服务器端实现。虽然我收到“已连接到APNS {”aps“:{”警报“:”嗨推送“,”徽章“:1,”声音“:”默认“}}消息已成功发送”消息,但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个答案

曹波鸿
2023-03-14

请检查证书是否匹配。例如,如果iphone处于调试模式,服务器也使用开发者证书发送推送通知,如果iphone是在发行版中构建的,则服务器使用生产证书发送推送到通知。请验证您的证书是否匹配。同时检查iPhone设置是否为应用程序启用了通知。

 类似资料:
  • 嗨,我正在为Windows Phone8开发。我需要发送和接收贴图推送通知使用PHP。我的设备上未收到平铺推送通知。我得到了下面给出的错误。有人能告诉我为什么我要面对这个问题吗? 错误: HTTP/1.1 200 OK缓存-控件:专用服务器:Microsoft-IIS/7.5 X-DeviceConnectionStatus:已连接X-NotificationStatus:已取消X-Subscri

  • 当我的应用程序处于打开状态时,我正在通过onMessageReceived(Remotemessage mesg)获得推送通知。如果我的应用程序处于Kilded状态,我将收到推送通知,但不是从onMessageReceived()获得的。 意思是,在收到推送通知后,根据通知中的数据,我需要重定向页面,当通知点击时。如果应用程序在前景工作良好。当我杀人的时候,我在托盘上收到通知,但当我点击通知时,它

  • Samsung正在成功获取应用程序在后台或前台被杀死, 我的目标是API27,这是我的代码 分级

  • 我没有收到Android推送通知。我能够 < li >注册设备 < li >接收设备标识符 < li >将通知消息推送到GCM 我成功进入通知发送事件,但我的设备上没有收到任何消息。成功的推送代码如下:

  • null 我还试了一下: 关闭应用程序进程-&>;关闭设备-&>;打开设备电源-&>;发送通知。。。并且没有收到通知! 看起来firebase只有在设备启动了应用程序而不是100%关闭的情况下才能收到通知,我的意思是,只需用后退键关闭它而不是关闭应用程序进程。 这怎么可能呢?据推测,即使应用程序关闭,firebase也应该收到通知。 我正在Nexus5X和Android 8.0上进行测试,我使用的

  • 当我点击it时,从服务器收到的通知应用程序已打开,但onNotification从未调用。我将其放入componentDidMount()中,并在App中放入构造函数。但是从来没人叫过。。帮助