你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS 消息推送及本地通知,原理解析

iOS 消息推送及本地通知,原理解析

編輯:IOS開發綜合

在此整理了一些前輩的思路,根據自己對問題的理解及相似方面的比較做了這篇筆記,本文並未詳細講解,只是根據自己提出問題進行一個解決,希望能對大家有些幫助。

1. deviceToken與UUID,UDID的區別



deviceToken :
A token that identifies the device to APS. The token is an opaque data type because that is the form that the provider needs to submit to the APS servers when it sends a notification to a device. The APS servers require a binary format for performance reasons.
Note that the device token is different from the uniqueIdentifier property of UIDevice because, for security and privacy reasons, it must change when the device is wiped.


當設備被擦除後,token必須變化。


UUID:Universally Unique IDentifiers,翻譯過來就是“全局唯一標志符”
UUID是一個標識你系統中的存儲設備的字符串,其目的是幫助使用者唯一的確定系統中的所有存儲設備,不管它們是什麼類型的。它可以標識DVD驅動器,USB存儲設備以及你系統中的硬盤設備等。一個典型的UUID看起來就是這樣:
BF930680-9994-442C-8A6C-FB4FD0707EB2(128bit 32位16進制)


為什麼使用UUID:
> 1.它是真正的唯一標志符
UUID為系統中的存儲設備提供唯一的標識字符串,不管這個設備是什麼類型的。如果你在系統中添加了新的存儲設備如硬盤,很可能會造成一些麻煩,比如說啟動的時候因為找不到設備而失敗,而使用了UUID則不會有這樣的問題。


> 2.設備名並非總是不變的
自動分配的設備名稱並非總是一致的,它們依賴於啟動時內核加載模塊的順序。如果你在插入了USB盤時啟動了系統,而下次啟動時又把它拔掉了,就有可能導致設備名分配不一致。
使用UUID對於掛載移動設備也非常有好處——例如我有一個24合一的讀卡器,它支持各種各樣的卡,而使用UUID總可以使用同一塊卡掛載在同一個地方。


> 3.ubuntu中的許多關鍵功能現在開始依賴於UUID





操作步驟前的一些原理:
device token,即設備令牌,不是系統唯一標識,需要在應用啟動時發起到apple服務器請求,注冊自己的設備和應用,並獲得這個device token。


device token有什麼用?如果應用需要push notification給手機,那麼它要有個服務器端(provider),但是它發出的信息不是直接給手機的,而是必須統一交給apple的服務器,這個服務器就是apple push notification server(apns)。apple服務器通過這個token,知道應用要發的消息是給哪個手機設備的,並轉發該消息給手機,手機再通知應用程序。


UDID:Unique Device Identifier,它是蘋果IOS設備的唯一標識碼,它由40個字符的字母和數字組成(越獄的設備通過某些工具可以改變設備的UDID)。移動網絡可利用UDID來識別移動設備,但是,從iOS5.0(2011年8月份)開始,蘋果宣布將不再支持用uniqueIdentifier方法獲取設備的UDID,iOS5以下是可以用的。在2013年3月21日蘋果已經通知開發者:從2013年5月1日起,訪問UDIDs的程序將不再被審核通過,替代的方案是開發者應該使用“在iOS6中介紹的Vendor或Advertising標識符”。所以UDID是絕對不能用了。


更多信息參見:http://zengrong.net/post/2152.htm




2. 推送的各個階段:
> app 注冊推送服務通知(向iOS)
> iOS系統向APNS服務器請求一個device token
> app接收到device token
> app向你自己的服務器發送token
> 當有些事情觸發你的推送服務時,向APNS發送一個推送通知。
> APNS發送這個推送通知到你的app


證書配備完成後:
> 在項目的AppDelegate中的didFinishLaunchingWithOptions方法中加入下面的代碼:


#define iOS8 [[UIDevice currentDevice] systemVersion].doubleValue >= 8.0




- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

// 要區分是否為ios8
#ifdef iOS8
UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil];
[application registerUserNotificationSettings:notiSettings];
#else
[application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound)];
#endif


> 添加接收信息部分
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSLog(@deviceToken);
}


-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// 確實接收到推送信息
NSLog(@%@,userInfo);
}


3. 本地通知 :一定要進入到後台後才能看到效果
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification != nil) {
NSDate *now = [NSDate new];
notification.fireDate = [now addTimeInterval:10.0];
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.alertBody = @要去吃晚飯了;
application.applicationIconBadgeNumber = 10; // 顯示到Icon的圖標右上角
[application scheduleLocalNotification:notification];
}


UILocalNotification的實例,主要有三類屬性
* scheduled time, 時間周期,用來指定iOS系統發送通知的日期和時間;
* notification type,通知類型,包括警告信息、動作按鈕的標題、應用圖標上的badge(數字標記)和播放的聲音;
* 自定義數據,本地通知可以包含一個dictionary類型的本地數據


4. 遠程通知(第3點上的所有通知皆為遠程通知)
設備的准備
首先要知道,push notification 只能在真機上運行的,無法在模擬器上使用,如果在模擬器上運行,在注冊時會有類似如下錯誤:
Error in registration. Error: Error Domain=NSCocoaErrorDomain Code=3010 remote notifications are not supported in the simulator UserInfo=0x5d249d0 {NSLocalizedDescription=remote notifications are not supported in the simulator}
真機也需要注意,如果沒有越獄,沒有問題。越獄的話,比如通過blacksnOw,因為沒有經過iTunes,無法生成有效的設備證書(device certificate),因此注冊的時候不會成功。

 
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved