轻松搞定iOS远程消息推送

2020-01-17 21:28:41王旭

最后一步,将我们生成的两个pem文件和成为一个:

$ cat PushCert.pem PushKey.pem > ck.pem

(2)测试证书是否可用

在终端执行下面的命令:

$ telnet gateway.sandbox.push.apple.com 2195

等一小会,如果终端显示下面的情形,则证书正常。

iOS远程推送配置,iOS远程推送,iOS远程推送原理,iOS远程推送过程

然后执行如下命令:

openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert PushChatCert.pem -key PushKey.pem
输入密码后回车显示如下的结果则连接成功:

iOS远程推送配置,iOS远程推送,iOS远程推送原理,iOS远程推送过程

3、天涯海角、一步之遥——应用程序中的配置

在我们项目的AppDelegate中添加如下代码:


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
double version = [[UIDevice currentDevice].systemVersion doubleValue];//判定系统版本。
if(version>=8.0f){
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert) categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    
  }else{
    UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
  }
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ // 处理推送消息
  NSLog(@"userinfo:%@",userInfo);
  NSLog(@"收到推送消息:%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *) error {
  NSLog(@"Registfail%@",error);
}
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
  NSLog(@"%@",deviceToken);//这里的Token就是我们设备要告诉服务端的Token码
}

下面是网上搜的PHP服务端的代码:


<?php
//这里填写设备的Token码
$deviceToken = '74314cc9e8f747e2fa96c2c1585c830cdf994de6b453ce9fa1c09ba396b2f9e9';
//这里是密钥密码
$passphrase = 'abcabc';
//推送的消息
$message = '这是一条推送消息';

////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');//ck文件
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;

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