Home php教程 php手册 IOS 推送消息 php做推送服务端

IOS 推送消息 php做推送服务端

Jun 06, 2016 pm 07:54 PM
http ios php Push Serve information

http://zxs19861202.iteye.com/blog/1532460 IOS推送消息是许多IOS应用都具备的功能,最近也在研究这个功能,参考了很多资料终于搞定了,下面就把步骤拿出来分享下: iOS消息推送的工作机制可以简单的用下图来概括: Provider是指某个iPhone软件的Push服务器

http://zxs19861202.iteye.com/blog/1532460


IOS推送消息是许多IOS应用都具备的功能,最近也在研究这个功能,参考了很多资料终于搞定了,下面就把步骤拿出来分享下:

 

 

 

iOS消息推送的工作机制可以简单的用下图来概括:


IOS 推送消息 php做推送服务端

 

Provider是指某个iPhone软件的Push服务器,APNS是Apple Push Notification Service的缩写,是苹果的服务器。

 

上图可以分为三个阶段:

第一阶段:应用程序把要发送的消息、目的iPhone的标识打包,发给APNS。 

第二阶段:APNS在自身的已注册Push服务的iPhone列表中,查找有相应标识的iPhone,并把消息发送到iPhone。 

第三阶段:iPhone把发来的消息传递给相应的应用程序,并且按照设定弹出Push通知。

IOS 推送消息 php做推送服务端

 

从上图我们可以看到:

1、应用程序注册消息推送。

2、iOS从APNS Server获取device token,应用程序接收device token。

3、应用程序将device token发送给PUSH服务端程序。

4、服务端程序向APNS服务发送消息。

5、APNS服务将消息发送给iPhone应用程序。

 

无论是iPhone客户端和APNS,还是Provider和APNS,都需要通过证书进行连接。

 

下面我介绍一下几种用到的证书。

 

一、CSR文件

 

1、生成Certificate Signing Request(CSR)


IOS 推送消息 php做推送服务端

 

2、填写你的邮箱和常用名称,并选择保存到硬盘。


IOS 推送消息 php做推送服务端

 

点击继续:


IOS 推送消息 php做推送服务端

 

这样就在本地生成了一个Push.certSigningRequest文件。

 

二、p12文件

 

1、导出密钥。


IOS 推送消息 php做推送服务端


IOS 推送消息 php做推送服务端

 

2、输入你的密码。

 

IOS 推送消息 php做推送服务端

 

这样就生成了一个Push.p12文件。

 

三、SSL certificate文件

 

1、用你付过费的帐号登录到iOS Provisioning Portal,并新建一个App ID,这个过程可以参考:iOS应用的真机调试,这样就会生成下面这条记录:


IOS 推送消息 php做推送服务端

 

2、点击右侧的Configure:


IOS 推送消息 php做推送服务端

 

3、点击Development Push SSL Certificate一行后的Configure:

 

IOS 推送消息 php做推送服务端

 

4、点击Continue:


IOS 推送消息 php做推送服务端

 

5、选择前面生成好的Push.certSigningRequest文件,点击Generate,出现如下所示的页面:


IOS 推送消息 php做推送服务端

 

6、点击Continue:


IOS 推送消息 php做推送服务端

 

 

7、点击Download,并将文件命名为aps_developer_identity.cer。

 

8、点击Done,你会发现状态变成了Enabled:


IOS 推送消息 php做推送服务端

 

 

到现在为止,我们已经生成了三个文件:

1、Push.certSigningRequest

2、Push.p12

3、aps_developer_identity.cer

 

双击aps_developer_dientity.cer 注册到你的钥匙串中,这样你的钥匙串中就会有


IOS 推送消息 php做推送服务端

二、准备profile证书,因为推送消息只能再真机上测试,所以要建一个profile证书


IOS 推送消息 php做推送服务端
点击"new profile"为上面新建的APP ID建个profile ,成功之后下载*_Dev_Profile.mobileprovision

双击将其加入到xcode 的Provisioning Profiles 中,这里有一点要注意,再将这个加入xcode之前如果之前已经加入过一定要把之前加入的删掉,如果有多个的话会出错。

 

三、工程代码

到这里证书已经准备完毕,接下来,我们在xcode中新建一个测试工程,注意设置工程的Bundle Identifier必须与上面建的APP ID 里的相同


IOS 推送消息 php做推送服务端

在didFinishLaunchingWithOptions 中加入一下代码

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

 

[self.window makeKeyAndVisible];

    [[UIApplication sharedApplicationregisterForRemoteNotificationTypesUIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];

    return YES;

 

}

 

 

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)pToken {


    NSLog(@"regisger success:%@", pToken);

    

    //注册成功,deviceToken保存到应用服务器数据库中

    

}


 

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{

    // 处理推送消息

    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"通知" message:@"我的信息" delegate:selfcancelButtonTitle:@"取消" otherButtonTitles:nilnil];

    [alert show];

    [alert release];

NSLog(@"%@", userInfo);

}


- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

    NSLog(@"Regist fail%@",error); 

    

  

}

 

到这里一切顺利的话我们就可以在真机运行了,注册成功我们会得到iphone 的deviceToken,

 

My token is:

 

四、在应用服务器采用php的方式将消息推送给APNS,

1、php连接APNS也是需要证书的,还记得我们上面获得的几个证书吗?打开终端,对上面的证书做如下处理,

cd  进入证书所在目录

 

把.cer文件转换成.pem文件:

$ openssl x509 -in aps_developer_identity.cer -inform der

-out PushChatCert.pem

把私钥Push.p12文件转换成.pem文件:

$ openssl pkcs12 -nocerts -out PushChatKey.pem -in Push.p12

Enter Import Password:

MAC verified OK

Enter PEM pass phrase:

Verifying – Enter PEM pass phrase:

你首先需要为.p12文件输入passphrase密码短语,这样OpenSSL可以读它。然后你需要键入一个新的密码短语来加密PEM文件。还是使用”pushchat”来作为PEM的密码短语。你需要选择一些更安全的密码短语。

注意:如果你没有键入一个PEM passphrase,OpenSSL将不会返回一个错误信息,但是产生的.pem文件里面将不会含有私钥。

最后。把私钥和证书整合到一个.pem文件里:

$ cat PushChatCert.pem PushChatKey.pem > ck.pem

为了测试证书是否工作,执行下面的命令

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

Trying 17.172.232.226…

Connected to gateway.sandbox.push-apple.com.akadns.net.

Escape character is ‘^]’.

它将尝试发送一个规则的,不加密的连接到APNS服务。如果你看到上面的反馈,那说明你的MAC能够到达APNS。按下Ctrl+C 关闭连接。如果得到一个错误信息,那么你需要确保你的防火墙允许2195端口。

然后再次连接,这次用我们的SSL证书和私钥来设置一个安全的连接:

$ openssl s_client -connect gateway.sandbox.push.apple.com:2195

-cert PushChatCert.pem -key PushChatKey.pem

Enter pass phrase for PushChatKey.pem:

你会看到一个完整的输出,让你明白OpenSSL在后台做什么。如果连接是成功的,你可以键入一些字符。当你按下回车后,服务就会断开连接。如果在建立连接时有问题,OpenSSL将会给你一个错误消息,

ck.pem文件就是我们需要得到php连接APNS 的文件,将ck.pem和push.php放入同一目录上传到服务器,push.php的代码如下:

 


// 这里是我们上面得到的deviceToken,直接复制过来(记得去掉空格

$deviceToken = '740f4707bebcf74f 9b7c25d4 8e3358945f6aa01da5ddb387462c7eaf 61bb78ad';


// Put your private key's passphrase here:

$passphrase = 'abc123456';


// Put your alert message here:

$message = 'My first push test!';


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


$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.push.apple.com:2195“, $err, $errstr, 60, //STREAM_CLIENT_CONNECT, $ctx);

//这个是沙盒测试地址,发布到appstore后记得修改哦

$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);

?>


接下来我们访问http://localhost/push/push.php


iphone就会接收到一条推送消息了,如果有问题的话就检查上面的操作步骤,特别是加红的部分

 

另外去除标记的方法为,在viewDidApper中加入

int badge = [UIApplication sharedApplication].applicationIconBadgeNumber;

    if(badge > 0)

    {

        badge--;

        [UIApplication sharedApplication].applicationIconBadgeNumber = badge;

    }

 

 

 

给一个参考地址:http://article.ityran.com/archives/194


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles