ios 本地通知

本文介绍如何在iOS应用中创建并设置本地通知,包括指定通知触发时间、设置时区、定义提示内容及声音等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 UILocalNotification *notification=[[UILocalNotification alloc] init];
     if (notification!=nil) {
           NSDate *now = [NSDate date];
             //从现在开始,10秒以后通知
             notification.fireDate=[now dateByAddingTimeInterval:10];
	 		//notification.fireDate = [NSDate dateWithTimeIntervalSince1970:8*60*60];//8点通知
             //使用本地时区
             notification.timeZone=[NSTimeZone defaultTimeZone];
             notification.alertBody=@"顶部提示内容,通知时间到啦";
             //通知提示音 使用默认的
             notification.soundName= UILocalNotificationDefaultSoundName;
             notification.alertAction=NSLocalizedString(@"你锁屏啦,通知时间到啦", nil);
            //这个通知到时间时,你的应用程序右上角显示的数字。
            notification.applicationIconBadgeNumber = 1;
             //add key  给这个通知增加key 便于半路取消。nfkey这个key是我自己随便起的。
             // 假如你的通知不会在还没到时间的时候手动取消 那下面的两行代码你可以不用写了。
             NSDictionary *dict =[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:notificationtag],@"nfkey",nil];
             [notification setUserInfo:dict];
             //启动这个通知
             [[UIApplication sharedApplication]   scheduleLocalNotification:notification];
            //这句真的特别特别重要。如果不加这一句,通知到时间了,发现顶部通知栏提示的地方有了,然后你通过通知栏进去,然后你发现通知栏里边还有这个提示
             //除非你手动清除,这当然不是我们希望的。加上这一句就好了。网上很多代码都没有,就比较郁闷了。
             [notification release];

}

//每1分钟提醒一次 
notification.repeatInterval= NSMinuteCalendarUnit; 
//每1小时提醒一次 
notification.repeatInterval= NSHourCalendarUnit; 
//每1天提醒一次 
notification.repeatInterval=NSDayCalendarUnit; 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值