添加本地推送
///本地添加
- -(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>