本地通知

本地通知使用注意:用户使用当前app的时候时接收不到这个app的通知的,app必须在后台或者结束才能发送通知

下面是一个简单的本地通知例子

- (void)viewDidLoad {
    [super viewDidLoad];
    [self localNotify];
    //把应用右上角提醒数字设置为0
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    
}


- (void)localNotify
{
    //1.创建通知对象
    UILocalNotification *localNotify = [[UILocalNotification alloc] init];
    //设置通知内容
    localNotify.alertBody = @"这是一个本地通知";
    //锁屏状态下的通知内容
    localNotify.alertAction = @"锁屏状态通知";
    //设置通知时间
    localNotify.fireDate = [NSDate dateWithTimeIntervalSinceNow:8];
    //重复多少个单元发送一次(一分钟发送一次,两天发送一次,一周发送一次)
    localNotify.repeatInterval = NSCalendarUnitMinute;
    //应用图标右上角提醒数字
    localNotify.applicationIconBadgeNumber = 5;
    
    //2.调度通知
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotify];
    
}

 

你可能感兴趣的:(本地通知)