本地推送UILocalNotification

时间:2023-03-08 20:03:49

//本地推送---无需网络,由本地发起

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

//设置推送时间间隔并开启推送fireDate

localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];

//设置提醒内容

localNotification.alertBody = @"注意要吃饭了";

//设置提醒标题

localNotification.alertTitle = @"提示";

//未读数图标显示badgeNumber

localNotification.applicationIconBadgeNumber = 2;

//定时推送

[[UIApplication shareApplication] scheduleLocalNotification:localNotification];

iOS8 以后需要添加如下步骤

//获取当前设备的系统版本UIDevice

if ([UIDevice currentDevice].systemVersion.floatValue > 8.0) {

UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];

//注册推送 以及 推送相关设置

[[UIApplication sharedApplication]registerUserNotificationSettings:setting];

}

//上文中设置

applicationIconBadgeNumber

在阅读完推送信息后应当清零

此时应该在AppDelegate文件中

设置

//已经收到推送信息后调用的方法

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{

application.applicationIconBadgeNumber = 0;

}