cocos2d游戏添加本地通知

来源:互联网 发布:类似知天下吧的资源站 编辑:程序博客网 时间:2024/05/15 09:25

最近游戏要上本地通知,其实就是UILocalNotification这个东东,它的功能实现比较简单,但是用途很广,最大的用途就是阶段性的让用户回归我们的游戏中,直接上代码:

- (void)applicationDidBecomeActive:(UIApplication *)application {    //从后台返回,先取消原有的通知再发送新的通知    NSLog(@"从后台返回,先取消原有的通知再发送新的通知!");    application.applicationIconBadgeNumber = 0;//应用程序右上角的数字=0(消失)      [[UIApplication sharedApplication] cancelAllLocalNotifications];//取消所有的通知        //通知;      UILocalNotification *notification=[[UILocalNotification alloc] init];       if (notification!=nil) {//判断系统是否支持本地通知          //notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:kCFCalendarUnitMinute];//本次开启立即执行的周期          //notification.repeatInterval=kCFCalendarUnitDay;//循环通知的周期          NSDate *now=[NSDate new];        notification.fireDate=[now dateByAddingTimeInterval:3600];//一小时后通知        notification.repeatInterval=0;//只提醒一次                notification.timeZone=[NSTimeZone defaultTimeZone];          notification.alertBody=@"哇,我的蘑菇,你怎了?";//弹出的提示信息          notification.applicationIconBadgeNumber=1; //应用程序的右上角小数字          notification.soundName= UILocalNotificationDefaultSoundName;//本地化通知的声音          notification.alertAction = NSLocalizedString(@"营救蘑菇!", nil);  //弹出的提示框按钮          [[UIApplication sharedApplication] scheduleLocalNotification:notification]; //登记UILocalNotification     }  [[CCDirector sharedDirector] resume];}

上面代码添加了一个通知,通知在游戏返回后一小时后激发,当然你可以根据需求添加N个,但是一定要设计好逻辑,否则通知就会乱蹦了!

最后附上UILocalNotification的相关介绍:

1、UILocalNotification的调度

其中fireDate、timeZone、repeatInterval和repeatCalendar是用于UILocalNotification的调度。fireDate是UILocalNotification的激发的确切时间。timeZone是UILocalNotification激发时间是否根据时区改变而改变,如果设置为nil的话,那么UILocalNotification将在一段时候后被激发,而不是某一个确切时间被激发。repeatInterval是UILocalNotification被重复激发之间的时间差,不过时间差是完全根据日历单位(NSCalendarUnit)的,例如每周激发的单位,NSWeekCalendarUnit,如果不设置的话,将不会重复激发。repeatCalendar是UILocalNotification重复激发所使用的日历单位需要参考的日历,如果不设置的话,系统默认的日历将被作为参考日历。

2、UILocalNotification的提醒内容

alertBody是一串现实提醒内容的字符串(NSString),如果alertBody未设置的话,Notification被激发时将不现实提醒。alertAction也是一串字符(NSString),alertAction的内容将作为提醒中动作按钮上的文字,如果未设置的话,提醒信息中的动作按钮将显示为“View”相对文字形式。alertLaunchImage是在用户点击提醒框中动作按钮(“View”)时,等待应用加载时显示的图片,这个将替代应用原本设置的加载图片。hasAction是一个控制是否在提醒框中显示动作按钮的布尔值,默认值为YES。

3、UILocalNotification的其他部分

applicationIconBadgeNumber、soundName和userInfo将使UILocalNotification更完整。applicationIconBadgeNumber是显示在应用图标右上角的数字,这样让用户直接了解到应用需要处理的Notification。soundName是另一个UILocalNotification用来提醒用户的手段,在Notification被激发之后将播放这段声音来提醒用户有Notification需要处理,如果不设soundName的话,Notification被激发是将不会有声音播放,除去应用特制的声音以外,也可以将soundName设为UILocalNotificationDefaultSoundName来使用系统默认提醒声音。userInfo是Notification用来传递数据的NSDictionary。


原创粉丝点击