cocos2d-x 添加本地通知UILocalNotification

本地通知UILocalNotification現在被大量用在遊戲設計中,目的在於召回用戶。一個合理的通知提示,或許能讓用戶重新回到你的遊戲中,那麼如何在cocos2d-x中添加本地通知呢?

其實很簡單,打開ios目錄下的AppController.mm文件,在application函數最下方添加如下代碼,最後結果如下:

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {      
  2.       
  3.     // Override point for customization after application launch.  
  4.   
  5.     // Add the view controller's view to the window and display.  
  6.       
  7.     //........      
  8.       
  9.     application.applicationIconBadgeNumber = 0;//應用程序右上角的數字=0(消失)  
  10.     [[UIApplication sharedApplication] cancelAllLocalNotifications];//取消所有的通知  
  11.     //------通知;  
  12.     UILocalNotification *notification=[[UILocalNotification alloc] init];   
  13.     if (notification!=nil) {//判斷系統是否支持本地通知  
  14.         NSDate *now=[NSDate new];   
  15.         //        notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:kCFCalendarUnitDay];//本次開啓立即執行的週期  
  16.         notification.fireDate=[now addTimeInterval:15];//本次開啓立即執行的週期  
  17.         notification.repeatInterval=kCFCalendarUnitMinute;//循環通知的週期  
  18.         notification.timeZone=[NSTimeZone defaultTimeZone];  
  19.         notification.alertBody=@"猴哥,要喫飯啦!";//彈出的提示信息  
  20.         notification.applicationIconBadgeNumber=1; //應用程序的右上角小數字  
  21.         notification.soundName= UILocalNotificationDefaultSoundName;//本地化通知的聲音  
  22.         notification.alertAction = NSLocalizedString(@"去喫飯", nil);  //彈出的提示框按鈕  
  23.         [[UIApplication sharedApplication]   scheduleLocalNotification:notification];  
  24.     }   
  25.     //本地通知 end  
  26.     return YES;  
  27. }  

這裏方便看效果,我把首次觸發的時間寫成15秒,這樣程序運行後,按Home鍵退到主屏幕,等15秒就可以看到效果了。

 

要同時啓動多個通知,只需要註冊多個就行了。

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