關於iOS的推送服務的點點,ios點點

來源:互聯網
上載者:User

關於iOS的推送服務的點點,ios點點

    蘋果推播通知服務(Apple Push Notification Service) 簡稱APNS. 是iOS應用中十分常見的一種互動方式. 

    APNS依託一個或幾個系統常駐進程運作,  接管所有應用的訊息推送, 所以可看作是獨立於應用之外, 而且是裝置和蘋果伺服器之間的通訊, 而非應用的供應商伺服器. 

    這和 Android不同, Android更像是傳統案頭電腦系統的做法. 每個需要後台推送的應用有各自的單獨後台進程, 才能和各自的伺服器通訊, 交換資料. 

    另外其實 Android 也有類似 APNS 的 GCM(Google Cloud Message),屬於開發人員可選,非強制.

    所以你大概看出來區別,iOS 的訊息推送機制面世之時是一種全新的解決方案(堪稱平台中的平台),應用本身不能有常駐的後台進程,系統的開銷少,記憶體使用量更少,電量也

更少(把更多的運算和資源開銷放在雲端,非裝置端)。而 Android 的特點,雖然開銷大,優點是更穩定快速,但不明顯。

    APNS的工作原理:

         1. App向裝置請求是否允許使用通知服務

         2. 裝置會攜帶App名向蘋果的推播通知伺服器發送允許接受通知服務的請求

         3. 蘋果推播通知伺服器將deviceToken和App名發送給裝置

         4. App將deviceToken等資訊發送至我們自己的儲存通知內容的伺服器

         5. 我們自己的儲存通知內容的伺服器將deviceToken和通知內容發送給蘋果推播通知伺服器

         6. 蘋果推播通知伺服器最後將通知內容按照deviceToken下發到裝置

    APNS的使用:

         1. 在https://developer.apple.com/account/ios/identifiers/bundle/bundleList.action中註冊App的id

         2. 在https://developer.apple.com/account/ios/certificate/certificateList.action?type=development中註冊APNS推送認證下載並安裝

         3. 一般的APNS都是在AppDelegate中進行.

            1). 首先要註冊通知服務, 由於iOS8.0之後的推播通知註冊方法改變, 所以要進行裝置的系統版本判斷

            // 註冊通知服務

            if ([UIDevice currentDevice].systemVersion.floatValue < 8.0) {

                [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |

                                                                                       UIRemoteNotificationTypeSound |

                                                                                       UIRemoteNotificationTypeAlert)];

            } else {

               UIUserNotificationSettings* settings = [UIUserNotificationSettings settingsForTypes:(

                                                                                          UIRemoteNotificationTypeBadge |

                                                                                          UIRemoteNotificationTypeSound |

                                                                                          UIRemoteNotificationTypeAlert) categories:nil];

              [application registerUserNotificationSettings:settings];

              [ application registerForRemoteNotifications];

           }

       4. 註冊成功調用的方法

         - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

       5. 註冊失敗調用的方法

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

       6. 接送推送訊息的觸發方法

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

   另: 推送中經常出現在icon上的角標 [UIApplication sharedApplication].applicationIconBadgeNumber

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.