iOS 本地通知
2015-10-09 21:19
435 查看
具体代码:
AppDelegate.m:
#pragma mark- 接收本地通知的方法
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
//接收到
通知之后的操作
NSLog(@"--------------%@",notification.userInfo);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:notification.alertTitle message:notification.alertBody delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
ViewController.m:
/*
本地通知 UILocalNotification
操作流程:
1.接收通知
2.注册
发送通知
提示事件
闹钟
在AppDelegate
里面实现代码
*/
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self pushNotfation];
}
#pragma mark-注册发送通知的方法
-(void)pushNotfation
{
//
注册
发送通知
//
初始化 alloc init
UILocalNotification *not = [[UILocalNotification alloc]init];
// fireDate
通知启动的时间
not.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
// 设置通知的标题
not.alertTitle = @"起床了";
// 设置通知的内容
not.alertBody = @"生前何必久睡,死后自会长眠";
// 通过通知
传递内容
not.userInfo = @{@"key":@"value"};
// 设置App
图标上面红点显示的数字
not.applicationIconBadgeNumber = 1; //在远程通知的时候
是系统提供给我们的
// 重复发送通知的方法
/*
NSCalendarUnitEra = kCFCalendarUnitEra,一个世纪
NSCalendarUnitYear = kCFCalendarUnitYear,
一年
NSCalendarUnitMonth = kCFCalendarUnitMonth,
一个月
NSCalendarUnitDay = kCFCalendarUnitDay,
天
NSCalendarUnitHour = kCFCalendarUnitHour,
时
NSCalendarUnitMinute = kCFCalendarUnitMinute,分
NSCalendarUnitSecond = kCFCalendarUnitSecond,秒
NSCalendarUnitWeekday = kCFCalendarUnitWeekday,
一个礼拜
NSCalendarUnitWeekdayOrdinal = kCFCalendarUnitWeekdayOrdinal,
*/
not.repeatInterval = kCFCalendarUnitDay;
//
注册通知
// 判断方法
是否可以响应某个方法
// [self respondsToSelector:<#(SEL)#>]
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
[[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert|UIUserNotificationTypeNone
categories:nil]];
// UIUserNotificationTypeBadge|
圆圈内提示的数字UIUserNotificationTypeSound|
通知提示的声音UIUserNotificationTypeAlert|
震动 UIUserNotificationTypeNone
}
not.soundName = UILocalNotificationDefaultSoundName;
//
发送通知
[[UIApplication sharedApplication]scheduleLocalNotification:not];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
AppDelegate.m:
#pragma mark- 接收本地通知的方法
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
//接收到
通知之后的操作
NSLog(@"--------------%@",notification.userInfo);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:notification.alertTitle message:notification.alertBody delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
ViewController.m:
/*
本地通知 UILocalNotification
操作流程:
1.接收通知
2.注册
发送通知
提示事件
闹钟
在AppDelegate
里面实现代码
*/
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self pushNotfation];
}
#pragma mark-注册发送通知的方法
-(void)pushNotfation
{
//
注册
发送通知
//
初始化 alloc init
UILocalNotification *not = [[UILocalNotification alloc]init];
// fireDate
通知启动的时间
not.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
// 设置通知的标题
not.alertTitle = @"起床了";
// 设置通知的内容
not.alertBody = @"生前何必久睡,死后自会长眠";
// 通过通知
传递内容
not.userInfo = @{@"key":@"value"};
// 设置App
图标上面红点显示的数字
not.applicationIconBadgeNumber = 1; //在远程通知的时候
是系统提供给我们的
// 重复发送通知的方法
/*
NSCalendarUnitEra = kCFCalendarUnitEra,一个世纪
NSCalendarUnitYear = kCFCalendarUnitYear,
一年
NSCalendarUnitMonth = kCFCalendarUnitMonth,
一个月
NSCalendarUnitDay = kCFCalendarUnitDay,
天
NSCalendarUnitHour = kCFCalendarUnitHour,
时
NSCalendarUnitMinute = kCFCalendarUnitMinute,分
NSCalendarUnitSecond = kCFCalendarUnitSecond,秒
NSCalendarUnitWeekday = kCFCalendarUnitWeekday,
一个礼拜
NSCalendarUnitWeekdayOrdinal = kCFCalendarUnitWeekdayOrdinal,
*/
not.repeatInterval = kCFCalendarUnitDay;
//
注册通知
// 判断方法
是否可以响应某个方法
// [self respondsToSelector:<#(SEL)#>]
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
[[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert|UIUserNotificationTypeNone
categories:nil]];
// UIUserNotificationTypeBadge|
圆圈内提示的数字UIUserNotificationTypeSound|
通知提示的声音UIUserNotificationTypeAlert|
震动 UIUserNotificationTypeNone
}
not.soundName = UILocalNotificationDefaultSoundName;
//
发送通知
[[UIApplication sharedApplication]scheduleLocalNotification:not];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
相关文章推荐
- iOS CollectionView
- iOS开发-进阶:代码块 block
- iOS 动力效果
- iOS开发寻找沙盒路径
- ios制作.a静态库
- iOS导航栏,标签视图的透明度处理
- iOS_触摸事件与手势识别
- GitHub 上大概600个开源 iOS 项目的分类和介绍,对于你挑选和使用开源项目应该有帮助
- nios ii avalon结构中的按键中断如何配置!
- iOS大典之Core Data
- 【iOS】本地推送和模拟远程推送
- iOS中的GCD(1)---串、并行队列和同、异步添加
- ios之viewWithTag
- NIOS II IDE编译出错:no file name for '-include'。Quartus ii中的.tcl文件run不起来
- iOS 禁止横屏
- iOS 地图与定位
- iOS SDK
- ios深度解析之coreData
- IOS开发:动画1
- 我的IOS学习历程-第二天