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

来源:互联网 发布:mac 建筑 软件 免费 编辑:程序博客网 时间:2024/05/22 03:25
一个每天固定时间本地通知的小代码

+ (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 1
原创粉丝点击