iOS遠程推送

來源:互聯網
上載者:User

標籤:

一.APNS 遠程推送

1.所有的蘋果裝置,在連網狀態下,都會與蘋果伺服器建立長串連.

2.長串連:就是只要連網了,就一直建立串連.

3.長串連的作用:時間校準,系統升級,尋找我的iPhone.

4.長串連的好處:資料轉送速度快,資料保持最新狀態.

5.  DeviceToken 處理流程如下:

1.手機註冊遠程通知到APNS伺服器.2.APNS伺服器向手機返回DeviceToken.3.手機再將DeviceToken發送到你的App.4.你的App再發送DeviceToken到資料供應商.

6. 擷取DeviceToken的流程需要檔案如下:

1.電腦真機調試認證(開發認證或發布認證).2.裝置的UUID.3.APPID需要Bundle identifier 也就是Xcode中的,必須是全名,不能有*號代替,如com.ling.identifier4.電腦描述檔案(CertificateSigningRequest.certSigningRequest檔案).
5.開發推送認證和發布推送認證.

 7.推送

PushMeBady是一款用來測試的APNs的開源Mac項目.github:https://github.com/stefanhafeneger/PushMeBaby

 8.APNS推送整體流程

1.建立推送相關認證.

2.在啟動AppDelegate方法中調用註冊遠程通知/*第2種app已經被關閉(進程已死) 啟動app,啟動完畢會調用AppDelegate的下面方法*/- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. //註冊遠程通知,iOS8.0以上的方法 UIUserNotificationSettings *settingNoti = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settingNoti]; [application registerForRemoteNotifications];

//iOS7.0及以下的方法// UIRemoteNotificationType type = UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound;// [application registerForRemoteNotificationTypes:type]; //進程已經被關閉(進程已死,再進來),擷取遠程通知內容 UILocalNotification *location = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]; if (location) { NSLog(@"%@",location.userInfo); }else{ NSLog(@"%@",location.userInfo); } return YES;}//註冊成功後會調用AppDelegate的下面方法,得到裝置的deviceToken- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{ NSLog(@"擷取deviceToken:%@",deviceToken); }/*當裝置接收到遠程推播通知時如果程式是處於關閉狀態,系統會在給使用者展示遠程推播通知的同時,將程式啟動到後台,並調用AppDelegate的下面方法*/- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{ if (userInfo) { NSDictionary *aps = userInfo[@"aps"]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"新的訊息" message: [NSString stringWithFormat:@"%@",aps[@"alert"]] delegate:self cancelButtonTitle:nil otherButtonTitles:@"取消", nil]; [alert show]; } }/*當使用者點擊遠程推播通知,會自動開啟app,這裡有2種情況第1種:app並沒有關閉,一直隱藏在後台讓app進入前台,並會調用AppDelegate的下面方法(並非重新啟動app*/- (void)application:(UIApplication *)application didReceiveRemoteNotification:(UILocalNotification *)notification{ NSLog(@"remoteNotification=%@",notification);}/*可以在這個方法中做些資料下載操作,爭取在使用者點擊通知前,就將資料下載完畢下載完畢要調用completionHandler這個block,告知下載完畢completionHandler(UIBackgroundFetchResultNewData);*/3.下載PushMeBadyDemo,測試遠程通知//注意使用PushMeBady時,在ioSock.h檔案中把不必要的錯誤注釋了如下面所示.//#include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacTypes.h>//電腦上匯入開發人員或發行者推送認證.修改名字為apns.cer//這裡填寫的擷取的DeviceTokenself.deviceToken = @"e3094258 d8766f77 f29dfbfa ec3e3e83 8f0c213e 01e0eeea cc040536 e7d41730"; //這裡是發布遠程通知內容 self.payload = @"{\"aps\":{\"alert\":\"星仔愛善良的女孩.\",\"badge\":1}}";//這裡是遠程通知APNs發布認證 self.certificate = [[NSBundle mainBundle] pathForResource:@"apns" ofType:@"cer"];

 二. 極光推送(JPush)

1.什麼是極光推送(JPush)

1.一套遠程推送解決方案,支援android和iOS兩個平台2.它能夠快捷地為iOS App增加推送功能,減少整合APNs需要的工作量、開發複雜度

2.步驟如下:

1.註冊帳號,建立應用.

 2.填寫應用資訊,開發認證和生產認證密碼是你匯出認證產生p12檔案的密碼.

3.匯出開發和發布(生產)推送認證,然後設定密碼

4.JPush會自動記錄和管理所有安裝過此app的裝置deviceToken,利用JPush,可以輕易地給所有裝置發送遠程推播通知.

未完待續.............

 

iOS遠程推送

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.