【iOS开发】UILocalNotification 本地通知的实现 —— 伊禾媛

来源:互联网 发布:html5 websocket php 编辑:程序博客网 时间:2024/05/22 11:35



//____viewController.m_____- (IBAction)swichChanged:(id)sender {        UISwitch *sw = (UISwitch *)sender;    if (sw.on) {        NSLog(@"打开");                //创建一个本地通知        UILocalNotification *notification = [[UILocalNotification alloc] init];        //设置通知的触发时间        notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];        //设置通知的时区        notification.timeZone = [NSTimeZone defaultTimeZone];        //设置通知重复发送的时间间隔        notification.repeatInterval = kCFCalendarUnitSecond;        //设置通知的声音        notification.soundName = @"msgcome.wav";        //设置当前设备处于锁屏状态是,显示通知的警告框下方的title        notification.alertAction = @"打开";        //设置通知是否可显示action        notification.hasAction = YES;        //设置通过通知加载应用时显示的图片        notification.alertLaunchImage = @"";        //设置通知的内容        notification.alertBody = @"我成功了";        //设置显示在应用程序上红色微标中的数字        notification.applicationIconBadgeNumber = 1;        //设置userInfo,用于谢爱额外的附加信息        NSDictionary *info = @{@"fkjava.org":@"key"};        notification.userInfo = info;        //调度通知        [app scheduleLocalNotification:notification];    }    else {        NSLog(@"关闭");            //获取所有处于调度中得本地通知数组        NSArray *localArray = [app scheduledLocalNotifications];        if (localArray) {            for (UILocalNotification *noti in localArray) {                                NSDictionary *dic = noti.userInfo;                if (dic) {                                        //如果找到要取消的通知                    NSString *inkey = [dic objectForKey:@"key"];                    if ([inkey isEqualToString:@"fkjava.org"]) {                                                //取消调度通知                        [app cancelLocalNotification:noti];                    }                }            }        }    }}


//_______appDelegate.m________- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {    //如果应用程序在前台运行,则将应用程序图标上的红色徽标中数字设为0    application.applicationIconBadgeNumber = 0;        //使用UIAlertView显示本地通知的信息    [[[UIAlertView alloc] initWithTitle:@"收到通知" message:notification.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];}- (void)applicationWillEnterForeground:(UIApplication *)application {    //当应用程序再次进入前台iyunxing时,将应用徽标数字设置为0    application.applicationIconBadgeNumber = 0;}


注意:iOS8 中发送本地通知需要添加如下代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {                UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];    }    return YES;}


运行效果分别为前台效果、后台效果、锁屏状态下效果。


0 0
原创粉丝点击