本地通知UILocalNotification現在被大量用在遊戲設計中,目的在於召回用戶。一個合理的通知提示,或許能讓用戶重新回到你的遊戲中,那麼如何在cocos2d-x中添加本地通知呢?
其實很簡單,打開ios目錄下的AppController.mm文件,在application函數最下方添加如下代碼,最後結果如下:
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- // Override point for customization after application launch.
- // Add the view controller's view to the window and display.
- //........
- application.applicationIconBadgeNumber = 0;//應用程序右上角的數字=0(消失)
- [[UIApplication sharedApplication] cancelAllLocalNotifications];//取消所有的通知
- //------通知;
- UILocalNotification *notification=[[UILocalNotification alloc] init];
- if (notification!=nil) {//判斷系統是否支持本地通知
- NSDate *now=[NSDate new];
- // notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:kCFCalendarUnitDay];//本次開啓立即執行的週期
- notification.fireDate=[now addTimeInterval:15];//本次開啓立即執行的週期
- notification.repeatInterval=kCFCalendarUnitMinute;//循環通知的週期
- notification.timeZone=[NSTimeZone defaultTimeZone];
- notification.alertBody=@"猴哥,要喫飯啦!";//彈出的提示信息
- notification.applicationIconBadgeNumber=1; //應用程序的右上角小數字
- notification.soundName= UILocalNotificationDefaultSoundName;//本地化通知的聲音
- notification.alertAction = NSLocalizedString(@"去喫飯", nil); //彈出的提示框按鈕
- [[UIApplication sharedApplication] scheduleLocalNotification:notification];
- }
- //本地通知 end
- return YES;
- }
這裏方便看效果,我把首次觸發的時間寫成15秒,這樣程序運行後,按Home鍵退到主屏幕,等15秒就可以看到效果了。
要同時啓動多個通知,只需要註冊多個就行了。