iOS本地通知的实现
2015-11-12 14:58
531 查看
/*
@property(nonatomic,copy) NSDate *fireDate; //
设置本地推送的时间
@property(nonatomic,copy) NSTimeZone *timeZone; //
时区
@property(nonatomic) NSCalendarUnit repeatInterval; //
重复多少个单元发出一次
@property(nonatomic,copy) NSCalendar *repeatCalendar; //
设置日期
@property(nonatomic,copy) CLRegion *region NS_AVAILABLE_IOS(8_0); //
比如某一个区域的时候发出通知
@property(nonatomic,assign) BOOL regionTriggersOnce NS_AVAILABLE_IOS(8_0); //进入区域是否重复
@property(nonatomic,copy) NSString *alertBody;
消息的内容
@property(nonatomic) BOOL hasAction;
是否显示alertAction的文字(默认是YES)
@property(nonatomic,copy) NSString *alertAction;
设置锁屏状态下,显示的一个文字
@property(nonatomic,copy) NSString *alertLaunchImage;
启动图片
@property(nonatomic,copy) NSString *soundName; UILocalNotificationDefaultSoundName
@property(nonatomic) NSInteger applicationIconBadgeNumber;
应用图标右上角的提醒数字
// user info
@property(nonatomic,copy) NSDictionary *userInfo;
*/
// 1.创建本地通知
UILocalNotification *localNote = [[UILocalNotificationalloc]
init];
// 1.1.设置什么时间弹出
localNote.fireDate = [NSDatedateWithTimeIntervalSinceNow:5];
// 1.2.设置弹出的内容
localNote.alertBody =
@"吃饭了吗?";
// 1.3.设置锁屏状态下,显示的一个文字
localNote.alertAction =
@"快点打开";
// 1.4.显示启动图片
localNote.alertLaunchImage =
@"123";
// 1.5.是否显示alertAction的文字(默认是YES)
localNote.hasAction =
YES;
// 1.6.设置音效
localNote.soundName =UILocalNotificationDefaultSoundName;
// 1.7.应用图标右上角的提醒数字
localNote.applicationIconBadgeNumber =999;
// 1.8.设置UserInfo来传递信息
localNote.userInfo =
@{@"alertBody" : localNote.alertBody,@"applicationIconBadgeNumber" :
@(localNote.applicationIconBadgeNumber)};
// 2.调度通知
[[UIApplicationsharedApplication]
scheduleLocalNotification:localNote];
iOS8 以后需要设置
/**
* 如果应用在后台,通过点击通知的时候打开应用会来到该代理方法
* 如果应用在前台,接受到本地通知就会调用该方法
*
* @param notification通过哪一个通知来这里
*/
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
// if (application.applicationState == UIApplicationStateActive) return;
// if (application.applicationState == UIApplicationStateInactive) {
// NSLog(@"跳转");
// }
UILabel *label = [[UILabelalloc]
init];
label.frame =
CGRectMake(0,
0, 300, 300);
label.backgroundColor = [UIColorredColor];
label.text = [NSStringstringWithFormat:@"%@", notification];
label.font = [UIFontsystemFontOfSize:14.0];
label.numberOfLines =
0;
[self.window.rootViewController.viewaddSubview:label];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/*
UIUserNotificationTypeNone = 0,
没有,没有本地通知
UIUserNotificationTypeBadge = 1 << 0,
接受图标右上角提醒数字
UIUserNotificationTypeSound = 1 << 1,
接受通知时候,可以发出音效
UIUserNotificationTypeAlert = 1 << 2,
接受提醒(横幅/弹窗)
*/
// iOS8需要添加请求用户的授权
if ([UIDevicecurrentDevice].systemVersion.floatValue
>=8.0) {
UIUserNotificationSettings *settings = [UIUserNotificationSettingssettingsForTypes:UIUserNotificationTypeBadge
|UIUserNotificationTypeAlert | UIUserNotificationTypeSoundcategories:nil];
[application registerUserNotificationSettings:settings];
}
// iOS 中本地通知启动的程序 launchOptions 的键名 UIApplicationLaunchOptionsLocalNotificationKey
会有值
if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
UILabel *label = [[UILabelalloc]
init];
label.frame =
CGRectMake(0,
300, 300, 300);
label.backgroundColor = [UIColorblueColor];
label.text = [NSStringstringWithFormat:@"%@", launchOptions];
label.font = [UIFontsystemFontOfSize:14.0];
label.textColor = [UIColorwhiteColor];
label.numberOfLines =
0;
[self.window.rootViewController.viewaddSubview:label];
}
return
YES;
}
@property(nonatomic,copy) NSDate *fireDate; //
设置本地推送的时间
@property(nonatomic,copy) NSTimeZone *timeZone; //
时区
@property(nonatomic) NSCalendarUnit repeatInterval; //
重复多少个单元发出一次
@property(nonatomic,copy) NSCalendar *repeatCalendar; //
设置日期
@property(nonatomic,copy) CLRegion *region NS_AVAILABLE_IOS(8_0); //
比如某一个区域的时候发出通知
@property(nonatomic,assign) BOOL regionTriggersOnce NS_AVAILABLE_IOS(8_0); //进入区域是否重复
@property(nonatomic,copy) NSString *alertBody;
消息的内容
@property(nonatomic) BOOL hasAction;
是否显示alertAction的文字(默认是YES)
@property(nonatomic,copy) NSString *alertAction;
设置锁屏状态下,显示的一个文字
@property(nonatomic,copy) NSString *alertLaunchImage;
启动图片
@property(nonatomic,copy) NSString *soundName; UILocalNotificationDefaultSoundName
@property(nonatomic) NSInteger applicationIconBadgeNumber;
应用图标右上角的提醒数字
// user info
@property(nonatomic,copy) NSDictionary *userInfo;
*/
// 1.创建本地通知
UILocalNotification *localNote = [[UILocalNotificationalloc]
init];
// 1.1.设置什么时间弹出
localNote.fireDate = [NSDatedateWithTimeIntervalSinceNow:5];
// 1.2.设置弹出的内容
localNote.alertBody =
@"吃饭了吗?";
// 1.3.设置锁屏状态下,显示的一个文字
localNote.alertAction =
@"快点打开";
// 1.4.显示启动图片
localNote.alertLaunchImage =
@"123";
// 1.5.是否显示alertAction的文字(默认是YES)
localNote.hasAction =
YES;
// 1.6.设置音效
localNote.soundName =UILocalNotificationDefaultSoundName;
// 1.7.应用图标右上角的提醒数字
localNote.applicationIconBadgeNumber =999;
// 1.8.设置UserInfo来传递信息
localNote.userInfo =
@{@"alertBody" : localNote.alertBody,@"applicationIconBadgeNumber" :
@(localNote.applicationIconBadgeNumber)};
// 2.调度通知
[[UIApplicationsharedApplication]
scheduleLocalNotification:localNote];
iOS8 以后需要设置
/**
* 如果应用在后台,通过点击通知的时候打开应用会来到该代理方法
* 如果应用在前台,接受到本地通知就会调用该方法
*
* @param notification通过哪一个通知来这里
*/
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
// if (application.applicationState == UIApplicationStateActive) return;
// if (application.applicationState == UIApplicationStateInactive) {
// NSLog(@"跳转");
// }
UILabel *label = [[UILabelalloc]
init];
label.frame =
CGRectMake(0,
0, 300, 300);
label.backgroundColor = [UIColorredColor];
label.text = [NSStringstringWithFormat:@"%@", notification];
label.font = [UIFontsystemFontOfSize:14.0];
label.numberOfLines =
0;
[self.window.rootViewController.viewaddSubview:label];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/*
UIUserNotificationTypeNone = 0,
没有,没有本地通知
UIUserNotificationTypeBadge = 1 << 0,
接受图标右上角提醒数字
UIUserNotificationTypeSound = 1 << 1,
接受通知时候,可以发出音效
UIUserNotificationTypeAlert = 1 << 2,
接受提醒(横幅/弹窗)
*/
// iOS8需要添加请求用户的授权
if ([UIDevicecurrentDevice].systemVersion.floatValue
>=8.0) {
UIUserNotificationSettings *settings = [UIUserNotificationSettingssettingsForTypes:UIUserNotificationTypeBadge
|UIUserNotificationTypeAlert | UIUserNotificationTypeSoundcategories:nil];
[application registerUserNotificationSettings:settings];
}
// iOS 中本地通知启动的程序 launchOptions 的键名 UIApplicationLaunchOptionsLocalNotificationKey
会有值
if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
UILabel *label = [[UILabelalloc]
init];
label.frame =
CGRectMake(0,
300, 300, 300);
label.backgroundColor = [UIColorblueColor];
label.text = [NSStringstringWithFormat:@"%@", launchOptions];
label.font = [UIFontsystemFontOfSize:14.0];
label.textColor = [UIColorwhiteColor];
label.numberOfLines =
0;
[self.window.rootViewController.viewaddSubview:label];
}
return
YES;
}
相关文章推荐
- 适配iOS 9系统
- iOS 保持界面流畅的技巧
- iOS开发:应用生命周期
- iOS 调用系统自带的通讯录
- ios多线程初步研究
- iOS_ NSUserDefaults_数据本地化的轻型装备
- iOS 键盘自适应(IQKeyboardManager)使用小结
- iOS 开始
- iOS9的一些适配
- iOS9的一些适配
- iOS PCH文件
- iOS使用推送流程
- iOS, ViewControl的生命周期
- iOS中三种数据存储(本地持久化),cell是手写创建和stroyboard或者xib创建的初始化的方法
- IOS四种保存文件的方式
- iOS中tableView的三种刷新方法
- iOS中Quartz2D
- iOS工程路径,路径宏
- iOS中Main函数作用,xcode7数据显示,自己设置主窗口
- IOS 开发,调用打电话,发短信,打开网址