您的位置:首页 > 移动开发 > IOS开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: