iOS本地通知——每天固定时间发送通知

一个每天固定时间本地通知的小代码

+ (void)registerLocalNotification{名字瞎取得不要在意这些细节。。

    UILocalNotification *notification = [[UILocalNotificationalloc]init];

    // 设置触发通知的时间


时间注意,since1970得到的是格林尼治时间早八点,所以需要几点通知算一下,你懂得,如下13*60*60代表延后13个小时也就是晚上21点

    NSDate *fireDate = [NSDatedateWithTimeIntervalSince1970:13*60*60];

    notification.fireDate = fireDate;

    // 时区

    notification.timeZone = [NSTimeZonedefaultTimeZone];

    // 设置重复的间隔

自行设定间隔思密达

    notification.repeatInterval =NSCalendarUnitDay;

    // 通知内容

    notification.alertBody@"你今天学习了嘛";

设置程序图标小红点数字,不知为何用++ \+=都只保持1,不过这并不重要(大概。。。

    notification.applicationIconBadgeNumber +=1;

    // 通知被触发时播放的声音

    notification.soundName =UILocalNotificationDefaultSoundName;

    // 通知参数

    NSDictionary *userDict = [NSDictionarydictionaryWithObject:@"test"forKey:@"key"];

    notification.userInfo = userDict;

    // 执行通知注册

    [[UIApplicationsharedApplication]scheduleLocalNotification:notification];


}


iOS8后请加如下语句,可以在程序启动就请求授权,也可以按需加到自己需要获取授权的时候

 //授权

    UIUserNotificationType type = UIUserNotificationTypeAlert |UIUserNotificationTypeBadge | UIUserNotificationTypeSound;

    UIUserNotificationSettings *settings = [UIUserNotificationSettingssettingsForTypes:typecategories:nil];

    [[UIApplicationsharedApplication]registerUserNotificationSettings:settings];


转自 http://blog.csdn.net/qq_35880238/article/details/52213778



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值