兩種方式 Local notifications 和 push notifications(也成爲Remote notifications)能夠讓當前沒有工做的App,給用戶提示信息。用戶的設備上會出現一個Alert,用戶能夠點擊進入該App。其中Push notifications適用於 iOS 3.0 及 Mac OS X v7.0, Local notifications 適用於 iOS 4。
比較簡單的應用,如捕魚達人。很長一段時間不玩的話,系統會彈出一個提示,提醒玩家很久沒有玩了。這是一種營銷方式。並且成本也不高。即是經過Local notifications來實現的。如今咱們就主要分析一下 Local notifications。至於Remote notifications,用於有服務器端支持的App。
UIApplication擴展了一個UILocalNotifications類別(一樣也有UIRemoteNotifications)
@interface UIApplication (UILocalNotifications)
它引入了一些方法如:
scheduleLocalNotification:
cancelLocalNotification:
用來註冊或取消一個UILocalNotification
假設咱們規定時間爲3天,若是3天沒有來玩,便彈出提示。那麼咱們首先應該在遊戲退出時註冊一個三天後觸發的事件。同時要在遊戲開始時,註銷掉這個事件。測試發現applicationWillTerminate並不會調用,多是ios4以後,用戶按下home鍵,當前應用並不退出而是轉向後臺的緣故。所以咱們將註冊位置放在了applicationDidEnterBackground裏。
NSDate *notifyTime = [NSDate dateWithTimeIntervalSinceNow:3*24*3600];//觸發的時間
UILocalNotification *localNofify = [[UILocalNotification alloc] init];
if(localNofify==nil)
return;
localNofify.fireDate = notifyTime;
localNofify.timeZone = [NSTimeZone defaultTimeZone];
localNofify.alertBody = @"您好久沒有玩了......";
localNofify.soundName = UILocalNotificationDefaultSoundName;
localNofify.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNofify];//註冊
[localNofify release];
須要說明的是iconBadgeNumber,它表明的是該應用的Icon上顯示的數字數。
在遊戲啓動時要記得清零:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
更多信息查看官方網站:http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008194-CH1-SW1html