[轉]some issues about push


custom data in push notification

http://no001.blog.51cto.com/1142339/642817


當運行帶apns feature的app到真機上後,會彈出錯誤:

"Error Domain=NSCocoaErrorDomain Code=3000 "no valid 'aps-environment' entitlement string found for application" UserInfo=0x15b200 {NSLocalizedDescription=no valid 'aps-environment' entitlement string found for application}" error.

是什麼原因?

檢查過下面幾點:

1. App ID 已經enable了apns service

2. Provisioning profile的創建是在app id enable apns service之後!

3. 進行調試的真機已經包含在provisioning profile裏

4. 沒辦法,死馬當活馬醫,刪除了舊的provisioning profile,再創建一個新的,添加新的到xcode organizer window


依然沒解決上面的問題。後來仔細看,原來是"building setting" tab裏的"code signing identity"我設置錯了


上圖中2個可選擇的item的name都一樣,但仔細看它們所屬的provisioning profile不同(見灰化部分),我錯誤的選擇了iOS team provisioning profile: * (for application identifiers *),應該選擇的是Mobile CAP Provisioning profile那個


在iphone鎖機的狀態下分3種情況:

1) 你的app沒有運行。這時如果來了push message,

解鎖後,就會自動運行該app。執行到application:didFinishLaunchingWithOptions:方法時notification info包含在參數launchOptions裏

2) 你的app運行在background。這時如果來了push message,

解鎖後,自動切換該app,並以notification info作爲參數調用application:didReceiveRemoteNotification:方法


3) 你的app運行在foreground。這時如果來了push message,

解鎖後,會以notification info作爲參數調用application:didReceiveRemoteNotification:方法



在iphone不是鎖機的狀態下分3種情況

1) 你的app沒有運行。這時如果來了push message,

> 如果你click "Launch" in alert notification window,或是在notification area裏click 與你的app相關的notification,那麼就會運行該app,執行到application:didFinishLaunchingWithOptions:方法時notification info包含在參數launchOptions裏

> 如果你click "Close" in alert notification window,或是沒有在notification area裏click 與你的app相關的notification,而是直接click app icon來打開app,執行到application:didFinishLaunchingWithOptions:方法時notification info並不會包含在參數launchOptions裏

2) 你的app運行在background。這時如果來了push message,

> 如果你click "Launch" in alert notification window,或是在notification area裏click 與你的app相關的notification,那麼就會切換到app,並以notification info作爲參數調用application:didReceiveRemoteNotification:方法

> 如果你click "Close" in alert notification window,或是沒有在notification area裏click 與你的app相關的notification,而是直接click app icon來打開app,則不會調用application:didReceiveRemoteNotification:方法

3) 你的app運行在foreground。這時如果來了push message,

Nothing happens on the screen and no sound is played,只會通過delegate來以notification info作爲參數來調用application:didReceiveRemoteNotification:方法


另外,在不是鎖機,當push message來的時候,

1)若之前app是沒有運行的,則執行到application:didFinishLaunchingWithOptions:方法時notification info並不會包含在參數launchOptions裏。

2)若之前app有在背景


注意:

目前好像無法通過代碼來控制你的app的notification狀態的開啓和關閉,user只能夠在settings > notification裏手動設置。而且你也無法在app裏獲取當然你的app的notificaiton的狀態是開啓還是關閉。


現在mobile cap app有一個需求是:若user不想每個push message都收到,而是隻收取每天下午5點的schedule push message。那麼不能夠通過disable整個notification,而應該在app設置一個選項來供user選擇是否只收5點的push message(by default is NO),當user修改該選項的值時,必須要能上網,因爲修改該選項應該send request to server來修改存儲在server的對應user的這個選項的值。server在push message時要判斷該值



收到push notification時,它會存儲在iPhone頂部的notification area。當你再次進入app後,那些屬於該app的notification不會從notification area裏刪除。怎樣才能刪除它
解決方法是:在application:didFinishLaunchingWithOptions:方法,application:didReceiveRemoteNotification:方法以及那些把app從後臺調到前臺trigger的方法(因爲如果不經notification切換app,就不會調用didReceiveRemoteNotification方法)裏適當的加上一行代碼來設置badge number=0
  1. [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];  
就會刪除notification area裏屬於該app的所有notifications。

注意,調用了上面那行之後,即使再設置badge number不爲0,
  1. [[UIApplication sharedApplication] setApplicationIconBadgeNumber:3];  
也會清除所有notifications

 

其實push notification對於iphone client side來說是很簡單的,只需要下面幾步:
1. 創建project時注意設置的bundle identifier必須和enable了push notification的App ID的bundle identifier一致
2. 在AppDelegate.m裏添加下列代碼,即可
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  2. {  
  3.     // Let the device know we want to receive push notifications  
  4.     [[UIApplication sharedApplication] registerForRemoteNotificationTypes:  
  5.      (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];  
  6.       
  7.     return YES;  
  8. }  
  9.   
  10. - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo  
  11. {  
  12.        //add codes to handle receive push notification event  
  13. }  
  14.   
  15. - (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken  
  16. {  
  17.     NSLog(@"My token is: %@", deviceToken);  
  18. }  
  19.   
  20. - (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error  
  21. {  
  22.     NSLog(@"Failed to get token, error: %@", error);  
  23. }  


APNS feedback service
If a provider attempts to deliver a push notification to an application, but the application no longer exists on the device, the device reports that fact to Apple Push Notification Service. This situation often happens when the user has uninstalled the application. If a device reports failed-delivery attempts for an application, APNs needs some way to inform the provider so that it can refrain from sending notifications to that device. Doing this reduces unnecessary message overhead and improves overall system performance.

For this purpose Apple Push Notification Service includes a feedback service that APNs continually updates with a per-application list of devices for which there were failed-delivery attempts.




APNS的Quality of Service
APNS包含一個default Quality of Service (QoS) component,它提供store-and-forward message的功能. 當APNs試圖push a notification to a device,但這時該device是offline時, QoS就會存儲該notification. 當該device再次online時,Qos就會發送存儲的notification給它。
對於Qos,要注意2點:
1. 它只能存儲一條notification,當有新的notification要存儲時,就會覆蓋舊的。總之,Qos總是存儲最新的那個notification.
2. Qos存儲的notification是有expire time (不清楚多久)的,當到時間後就會刪除它,這時device online也不會收到它。

ref link:
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章