iOS 本地推送
来源:互联网 发布:龙腾世纪起源知乎 编辑:程序博客网 时间:2024/05/29 21:16
// 设置本地通知
+ (void)registerLocalNotification:(NSInteger)alertTime;
+ (void)cancelLocalNotificationWithKey:(NSString *)key;
2.在.m文件中实现两个方法,可以创建button点击事件实现调用 + (void)registerLocalNotification:(NSInteger)alertTime;这个方法
// 设置本地通知alertTime为多久以后执行本地推送
+ (void)registerLocalNotification:(NSInteger)alertTime {
UILocalNotification *notification = [[UILocalNotificationalloc]init];
// 设置触发通知的时间
NSDate *fireDate = [NSDatedateWithTimeIntervalSinceNow:alertTime];
NSLog(@"fireDate=%@",fireDate);
notification.fireDate = fireDate;
// 时区
notification.timeZone = [NSTimeZonedefaultTimeZone];
// 设置重复的间隔
notification.repeatInterval =kCFCalendarUnitSecond;
// 通知内容
notification.alertBody = @"本地推送";
notification.applicationIconBadgeNumber =1;
// 通知被触发时播放的声音
notification.soundName =UILocalNotificationDefaultSoundName;
// 通知参数
NSDictionary *userDict = [NSDictionarydictionaryWithObject:@"本地推送实现"forKey:@"key"];
notification.userInfo = userDict;
// ios8后,需要添加这个注册,才能得到授权
if ([[UIApplicationsharedApplication]respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationType type = UIUserNotificationTypeAlert |UIUserNotificationTypeBadge |UIUserNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettingssettingsForTypes:type
categories:nil];
[[UIApplicationsharedApplication]registerUserNotificationSettings:settings];
// 通知重复提示的单位,可以是天、周、月
notification.repeatInterval =NSCalendarUnitDay;
} else {
// 通知重复提示的单位,可以是天、周、月
notification.repeatInterval =NSDayCalendarUnit;
}
// 执行通知注册
[[UIApplicationsharedApplication]scheduleLocalNotification:notification];
}
// 取消某个本地推送通知
+ (void)cancelLocalNotificationWithKey:(NSString *)key {
// 获取所有本地通知数组
NSArray *localNotifications = [UIApplicationsharedApplication].scheduledLocalNotifications;
for (UILocalNotification *notificationin localNotifications) {
NSDictionary *userInfo = notification.userInfo;
if (userInfo) {
//根据设置通知参数时指定的key来获取通知参数
NSString *info = userInfo[key];
// 如果找到需要取消的通知,则取消
if (info !=nil) {
[[UIApplicationsharedApplication]cancelLocalNotification:notification];
break;
}
}
}
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;中调用
// 取消某个本地推送通知
NSString *notMess = [notification.userInfoobjectForKey:@"key"];
UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"本地通知(前台)"
message:notMess
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
// 更新显示的徽章个数
NSInteger badge = [UIApplicationsharedApplication].applicationIconBadgeNumber;
badge--;
badge = badge >= 0 ? badge :0;
[UIApplicationsharedApplication].applicationIconBadgeNumber = badge;
在调用+ (void)cancelLocalNotificationWithKey:(NSString *)key;这个方法
- iOS推送:实现本地推送
- iOS推送:实现本地推送
- iOS推送:实现本地推送
- iOS推送:实现本地推送
- ios (推送)之本地推送
- iOS推送:实现本地推送
- ios (推送)之本地推送
- iOS推送之本地推送
- ios推送之本地推送
- ios本地推送
- iOS本地推送
- iOS 本地推送
- ios本地推送方法
- IOS本地推送处理
- iOS -- 本地推送
- IOS本地推送通知
- iOS 本地推送
- ios本地推送
- shell 脚本之特殊变量字符
- StickerView实现View的拖拽,放大,缩小。
- linux如何查看端口被哪个进程占用
- 云+网+端是互联网的基本结构
- html语义化标签
- iOS 本地推送
- Remove Duplicates from Sorted Array(重复1次或2次)--Array
- MySQL字符集和校验规则概
- python,排列组合
- Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)
- ubuntu linux eclipse 集成hadoop开发环境
- Matlab与线性代数 -- 矩阵的重组3
- 关于GreenDao数据库框架在android studio上的相关配置和使用
- LayoutCreator