无法从我的php接收推送通知消息,但在线测试网站推送可以获取消息

时间:2017-05-20 01:26:36

标签: ios notifications push

我无法从我的php获取推送通知,但我可以从online site for testing收到通知。

我的iOS项目步骤如下:

  1. 为“推送通知”添加设置功能更改为开启,背景模式选择“远程通知”和“背景提取”。

  2. 添加“UserNotifications.frameworks”

  3. 在AppDelegate中添加“”

  4. 4.在Appdelegate.m中添加以下代码:

     - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self;
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
        if( !error )
        {
            [[UIApplication sharedApplication] registerForRemoteNotifications];  // required to get the app to do anything at all about push notifications
            NSLog( @"Push registration success." );
        }
        else
        {
            NSLog( @"Push registration FAILED" );
            NSLog( @"ERROR: %@ - %@", error.localizedFailureReason, error.localizedDescription );
            NSLog( @"SUGGESTIONS: %@ - %@", error.localizedRecoveryOptions, error.localizedRecoverySuggestion );
        }
    }];
    
    [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
        NSLog(@"push settings:%@",settings);
    }];
    
    return YES;
     }
    
     - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken NS_AVAILABLE_IOS(3_0){
    
    NSString *deviceTokenString = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@""]];
    deviceTokenString = [deviceTokenString stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSLog(@"device token:%@",deviceTokenString);
    
     }
    
    
     -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
    
         UNNotificationRequest *request = notification.request;
    
            UNNotificationContent *content = request.content;
    
            NSDictionary *userInfo = content.userInfo;
    
            NSNumber *badge = content.badge;
    
            NSString *body = content.body;
    
    UNNotificationSound *sound = content.sound;
    
    NSString *subtitle = content.subtitle;
    
    NSString *title = content.title;
    
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
    
        NSLog(@"iOS10 get remote notify:%@",userInfo);
    
    }else {
    
        NSLog(@"iOS10 get local notify:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo);
    }
    
    
    completionHandler(UNNotificationPresentationOptionBadge|
                      UNNotificationPresentationOptionSound|
                      UNNotificationPresentationOptionAlert);
     }
    
        // notify click event
     - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
    
    UNNotificationRequest *request = response.notification.request;
    
    UNNotificationContent *content = request.content;
    
    NSDictionary *userInfo = content.userInfo;
    
    NSNumber *badge = content.badge;
    
    NSString *body = content.body;
    
    UNNotificationSound *sound = content.sound;
    
    NSString *subtitle = content.subtitle;
    
    NSString *title = content.title;
    
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        NSLog(@"iOS10 remote notify:%@",userInfo);
    
    
    }else {
                NSLog(@"local notify:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo);
    }
    
    
    completionHandler();
     }
    
    1. 我可以得到以下日志:
    2.   

      推送设置UNNotificationSettings 0x170089510:   authorizationStatus,Authorized,notificationCenterSetting,Enabled,   soundSetting,Enabled,badgeSetting:Enabled,lockScreenSetting:   已启用,alertSetting:NotSupported,carPlaySetting:已启用,   alertStyle,Alert

           

      推动注册成功。

           

      设备令牌:我的设备令牌ID

      1. 我设置了如下的PHP代码:
      2. <?php
        
        
         $deviceToken = $row['my device token ID'];
        // Put your private key's passphrase 
        $passphrase = 'my password';
        $message = 'My first push notification!';
        ////////////////////////////////////////////////////////////////////////////////
        $ctx = stream_context_create();
        stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
        stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
        stream_context_set_option($ctx, 'ssl', 'cafile', 'entrust_2048_ca.cer');
        // 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);
        // $fp = stream_socket_client(
        //  'ssl://gateway.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;
        // Create the payload body
        $body['aps'] = array('alert' => $message,'sound' => 'default');
        // Encode the payload as JSON
        $payload = json_encode($body);
        // 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);
        ?>
        
        1. 然后我在我的mac中运行php,运行localhost / push.php,我可以从网上获取消息:

          已成功发送连接到APNS消息

        2. 但我无法在我的应用中获取任何日志,也无法收到消息通知。

        3. 我使用开发证书和配置,所以我的测试推送站点设置为gateway.sandbox.push.apple.com:2195。

          有人知道为什么我无法收到通知,但我可以从在线测试网站获得通知吗?

          非常感谢。

1 个答案:

答案 0 :(得分:0)

我记得曾经遇到过同样的问题,而且我不太确定我是如何解决它的,因为我之后选择使用OneSignal,因为我没有后端开发人员为我编写代码。 但是,您是否可以尝试使用ssl替换代码的第5,6和7行中的tls并尝试?

相关问题