iOS本地通知 UILocalNotification

+ (void)registerLocalNotification:(NSDictionary *)dic

{

    NSString *fireDateStr =[dic objectForKey:@"fireDate"];

    NSInteger fireDate = [fireDateStr intValue];

    NSString *alertBody = [dic objectForKey:@"alertBody"];

    NSString *alertAction = [dic objectForKey:@"alertAction"];

    NSString * badgeNumberStr =[dic objectForKey:@"badgeNumber"];

    NSInteger badgeNumber = [badgeNumberStr intValue];

    NSString *keyName = [dic objectForKey:@"keyName"];

    NSInteger notificationtag =[dic objectForKey:@"notificationtag"];

    

    UILocalNotification *notification=[[UILocalNotification alloc] init];

    if (notification!=nil) {

        

        //使用本地时区

        notification.timeZone=[NSTimeZone timeZoneWithAbbreviation:@"GMT+0800"];//[NSTimeZone defaultTimeZone];

        NSDate *notifyTime = [NSDate dateWithTimeIntervalSinceNow:fireDate];//触发的时间

        //从现在开始,10秒以后通知

        notification.fireDate=notifyTime;

        

        notification.alertBody=alertBody;//@"顶部提示内容,通知时间到啦";

        //通知提示音 使用默认的

        notification.soundName= UILocalNotificationDefaultSoundName;

        notification.alertAction=NSLocalizedString(alertAction, nil);//@"你锁屏啦,通知时间到啦"

        //这个通知到时间时,你的应用程序右上角显示的数字。

        notification.applicationIconBadgeNumber = badgeNumber;

        //add key  给这个通知增加key 便于半路取消。nfkey这个key是我自己随便起的。

        // 假如你的通知不会在还没到时间的时候手动取消 那下面的两行代码你可以不用写了。

        NSDictionary *dict =[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:notificationtag],keyName,nil];

        [notification setUserInfo:dict];

        //启动这个通知

        [[UIApplication sharedApplication]   scheduleLocalNotification:notification];

        //这句真的特别特别重要。如果不加这一句,通知到时间了,发现顶部通知栏提示的地方有了,然后你通过通知栏进去,然后你发现通知栏里边还有这个提示

        //除非你手动清除,这当然不是我们希望的。加上这一句就好了。网上很多代码都没有,就比较郁闷了。

        [notification release];

    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值