添加本地推送
///本地添加
- -(void)addLocalPushNotification:(UIButton*)sender;
- {
-
-
- NSLog(@"%s",__FUNCTION__);
- UILocalNotification* localNotification=[[UILocalNotification alloc]init];
-
- if (localNotification) {
-
- NSDate* pushDate=[NSDate dateWithTimeIntervalSinceNow:20];
-
- localNotification.timeZone=[NSTimeZone defaultTimeZone];
-
- localNotification.fireDate=pushDate;
-
- localNotification.repeatInterval=kCFCalendarUnitDay;
-
- localNotification.soundName=UILocalNotificationDefaultSoundName;
-
- localNotification.alertBody=@"Hello world";
-
- localNotification.alertLaunchImage=[[NSBundle mainBundle]pathForResource:@"3" ofType:@"jpg"];
-
-
- NSDictionary* infoDic=[NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
- localNotification.userInfo=infoDic;
-
-
- UIApplication* app=[UIApplication sharedApplication];
- BOOL status=YES;
- for (UILocalNotification* notification in app.scheduledLocalNotifications) {
- if ([notification.userInfo objectForKey:@"key"]) {
- status=NO;
- }
- }
-
- if (status) {
-
- [app scheduleLocalNotification:localNotification];
- }
-
-
-
- NSLog(@"%@",app.scheduledLocalNotifications);
- }
- }
取消本地推送
- -(void)removeLocalPushNotification:(UIButton*)sender
- {
- NSLog(@"%s",__FUNCTION__);
- UIApplication* app=[UIApplication sharedApplication];
-
- NSArray* localNotifications=[app scheduledLocalNotifications];
-
- if (localNotifications) {
-
- for (UILocalNotification* notification in localNotifications) {
-
- NSDictionary* dic=notification.userInfo;
-
- if (dic) {
- NSString* key=[dic objectForKey:@"key"];
- if ([key isEqualToString:@"name"]) {
-
- [app cancelLocalNotification:notification];
-
- break;
- }
- }
-
- }
- }
-
-
-
-
- }
远程推送
当服务端远程向APNS推送至一台离线的设备时,苹果服务器Qos组件会自动保留一份最新的通知,等设备上线后,Qos将把推送发送到目标设备上
客户端需要注意的
bundle ID与App Id一致
设备Token能正常获取
若为沙盒测试,证书得使用developer的
单设备
如上图所示:我们的服务端将需要推送的相关信息提交到APNS(Apple Push Notification Service),由APNS在Push服务IOS设备列表中找到对应的设备,并将信息推到终端上,终端上再推到客户端APP上
多设备
流程大概是这样的
1.生成CertificateSigningRequest.certSigningRequest文件
2.将CertificateSigningRequest.certSigningRequest上传进developer,导出.cer文件
3.利用CSR导出P12文件
4.需要准备下设备token值(无空格)
5.使用OpenSSL合成服务器所使用的推送证书
1.打开钥匙串,在右上角选择(钥匙串访问->证书助理->从证书颁发机构请求证书)
生成 CertificateSigningRequest.certSigningRequest
以下信息填写号后,保存到对应位置
2.进入developer.apple.com中 上传CertificateSigningRequest.certSigningRequest并保存cer文件
(1)
(2)选择类型为 推送服务--沙盒测试用
(3)选中对应的APP ID,别忘了,项目配置文件中的Bundle ID与其一致
(4)选择保存路径
(5)选择上传文件 CertificateSigningRequest.certSigningRequest
(6)保存cer文件,并双击添加进钥匙串
(7)新建一个Provisioning Profiles
选中与前面一致的 App Id
选中刚才新建的certificates
选择可调试设备
保存provisioning文件,并将其加入设备中
通过OPENSSL文件合并
1.在钥匙串->证书 找到刚才所添加进去的证书 右键导出p12
2.进入终端 ,将aps_development.cer转成PushChatCert.pem(openssl x509 -in aps_development.cer -inform der -out PushChatCert.pem)
3.openssl pkcs12 -nocerts -out PushChatKey.pem -in Push.p12 生成p12私钥 .pem文件(需设置密码,服务端推送时要用)
4.利用PushChatCert.pem和新生成的PushChatKey.pem合成一个新的p12文件(这个p12是提供给服务器推送用的)(
openssl pkcs12 -export -in PushChatCert.pem -inkey PushChatKey.pem -certfile CertificateSigningRequest.certSigningRequest -name "aps_developer_identity" -out aps_developer_identity.p12
)
合成PHP所用的PEM文件
- openssl x509 -in aps_development.cer -inform der -out PushChatCert.pem
- openssl pkcs12 -nocerts -out PushChatKey.pem -in Push.p12
- cat PushChatCert.pem PushChatKey.pem > newck.pem
代码实现如下
注册推送通知
- [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
- (UIRemoteNotificationTypeAlert|
- UIRemoteNotificationTypeBadge|
- UIRemoteNotificationTypeSound)];
在AppDelegate中加入以下几个代理方法
- -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
- {
-
- NSLog(@"%@",deviceToken);
-
- }
- -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
- {
-
- NSLog(@"%@",error);
- }
- -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
- {
- for (id key in userInfo) {
- NSLog(@"%@:%@",key, [userInfo objectForKey:key]);
- }
-
- }
应用程序不处在后台,且通过推送通知打开的时候,如果需要推送下来相关的信息可以在
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
方法中加入
- if (launchOptions) {
-
- NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
- }
服务端PHP推送代码
- <?php
- $deviceToken= ‘ba6d5106503c8e62e68b5df1b36c3b58ced1588c6dabe0fc9e6828961aeb12d6‘;
- $body = array("aps" => array("alert" => ‘helloHui‘,"badge" => 2,"sound"=>‘default‘));
- $ctx = stream_context_create();
-
-
-
- stream_context_set_option($ctx,"ssl","local_cert","26ck.pem");
- $pass = "123123";
- stream_context_set_option($ctx, ‘ssl‘, ‘passphrase‘, $pass);
-
- $fp = stream_socket_client("ssl://gateway.sandbox.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
- if (!$fp) {
- echo "Failed to connect $err $errstrn";
- return;
- }
- print "Connection OK\n";
- $payload = json_encode($body);
- $msg = chr(0) . pack("n",32) . pack("H*", str_replace(‘ ‘, ‘‘, $deviceToken)) . pack("n",strlen($payload)) . $payload;
- echo "sending message :" . $payload ."\n";
- fwrite($fp, $msg);
- fclose($fp);?>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
-
- <div style="padding-top:20px">
- <p style="font-size:12px;">版权声明:本文为博主原创文章,未经博主允许不得转载。</p>
- </div>