iOS日常需求:UILocalNotification本地推送的使用详解
来源:互联网 发布:xp系统查找mac地址 编辑:程序博客网 时间:2024/05/22 06:57
UILocalNotification在实际的项目使用中用的还是比较多的,刚好在项目中用了一下,发现一些问题总结出来跟大家分享一下!
1、首先封装本地推送的设置和退出方法
+ (void)registerLocalNotification:(NSInteger)alertTime Sting:(NSString* )str{
UILocalNotification *notification = [[UILocalNotificationalloc]init];
//设置触发通知的时间
NSDate *fireDate = [NSDatedateWithTimeIntervalSinceNow:alertTime];
NSLog(@"fireDate=%@",fireDate);
notification.fireDate = fireDate;
// 时区
notification.timeZone = [NSTimeZonedefaultTimeZone];
// 设置重复的间隔
notification.repeatInterval =kCFCalendarUnitSecond;
// 通知内容
notification.alertBody = str;
notification.applicationIconBadgeNumber =1;
//通知被触发时播放的声音
notification.soundName =UILocalNotificationDefaultSoundName;
// 通知参数
NSDictionary *userDict = [NSDictionarydictionaryWithObject:strforKey:@"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;
}
}
}
}
2、在AppDelegate中引入封装好的类的头文件在didFinisLaunchingWithOptions函数中注册本地通知的背景数字和提示声音
[[UIApplicationsharedApplication]registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound)];
3、在本地推送的响应函数中实现弹出内容// 本地通知回调函数,当应用程序在前台时调用
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
//这里真实需要处理交互的地方
//获取通知所带的数据
NSString *notMess = [notification.userInfoobjectForKey:@"key"];
MozTopAlertView* topView = [MozTopAlertViewshowWithType:MozAlertTypeInfotext:notMessparentView:self.window.viewForBaselineLayout];
UITapGestureRecognizer* tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(topAlertAction)];
[topView addGestureRecognizer:tap];
//更新显示的徽章个数
NSInteger badge = [UIApplicationsharedApplication].applicationIconBadgeNumber;
badge--;
badge = badge >=0 ? badge :0;
[UIApplicationsharedApplication].applicationIconBadgeNumber = badge;
UIButton* btn = [[UIButtonalloc]init];
//在不需要再推送时,可以取消推送
[PXHHomeNotificationcancelLocalNotificationWithKey:@"key"];
}
4、然后在触发事件中调用本地推送事件[classNameregisterLocalNotification:0Sting:currentContent];//数字为响应时间间隔 string为显示内容
- iOS日常需求:UILocalNotification本地推送的使用详解
- iOS本地推送UILocalNotification详解
- iOS 本地推送 UILocalNotification
- iOS 本地推送 UILocalNotification
- iOS本地通知推送使用:UILocalNotification
- 关于本地推送的使用处理UILocalNotification
- ios推送:本地通知UILocalNotification
- ios推送:本地通知UILocalNotification
- ios推送:本地通知UILocalNotification
- ios推送:本地通知UILocalNotification
- ios推送:本地通知UILocalNotification
- iOS 本地推送通知 UILocalNotification
- ios推送:本地通知UILocalNotification
- iOS推送:本地通知UILocalNotification
- iOS推送:本地通知UILocalNotification
- iOS推送:本地通知UILocalNotification
- iOS --- 本地推送通知 UILocalNotification
- iOS推送:本地通知UILocalNotification
- git 常用命令
- 啊哈 phaser制作小游戏flappy bird(2)
- POJ - 3264 Balanced Lineup (RMQ问题求区间最值)
- 队列
- poj 1502 MPI Maelstrom (dij)
- iOS日常需求:UILocalNotification本地推送的使用详解
- Python函数式编程指南(一):概述
- PAT (Basic Level) Practise (中文)1040. 有几个PAT(25) C语言
- C++类保护函数外部调用
- POJ1190(搜索加剪枝)
- java 基础之枚举
- linux C 正则表达式 blog.chinaunix.net/uid-20729605-id-1884365.html
- 从头到尾彻底解析哈希表算法
- 八大排序算法-冒泡排序