Scheduling Local Notifications(调度本地通知)
来源:互联网 发布:finalcaption字幕软件 编辑:程序博客网 时间:2024/05/16 01:18
即使app不在运行,或是在后台运行,也可以实现在某一特定时刻,发送通知给用户,这些通知还可以循环发送,如在每周的特定时间(类似推送)
一般可显示为Banner或是Alert形式,目前以Banner为主,即顶端横条,在ios5及以上的版本中,本地通知的显示形式已不能更改(可在设置-通用-通知中心中查看)
实现:
初始化:UILocalNotification *notification = [[UILocalNotification alloc] init];
配置通知属性:fireDate:通知的激发日期(NSDate),timeZone:时区(NSTimeZone)
alertBody:通知内容
当通知以提醒框形式显示时—— hasAction:是否在提醒对话框中显示动作按钮文本
alertAction: 当hasAction= NO:nil
当hasAction=YES:标识动作按钮文本
applicationIconBadgeNumber:显示在应用图标右上角的数字,表示需要处理的通知数量
userInfo:可从中获取到与本地通知相关的字典信息(NSDictionary)
启动:- (void)scheduleLocalNotification:(UILocalNotification *)notification
取消:- (void)cancelAllLocalNotifications
e.g.
//在配置完通知的各项属性后,启动通知
- (void) scheduleLocalNotification{
UILocalNotification *notification = [[UILocalNotification alloc] init];
//触发时间
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:8.0];
//时区
notification.timeZone =[[NSCalendar currentCalendar] timeZone];
//通知内容
notification.alertBody = @"已经到。。。的时间了";
// 提醒框设置 -是否在提醒对话框中显示动作按钮文本
notification.hasAction = YES;
//提醒框设置 -提醒对话框中显示的动作按钮文本
notification.alertAction = @"打开";
//应用图标右上角的数字
int badgeNumber =[UIApplication sharedApplication].applicationIconBadgeNumber;
notification.applicationIconBadgeNumber =badgeNumber+ 1;
// 传递给用户的字典信息
notification.userInfo = @{@"Key 1": @"Value 1",
@"Key 2" : @"Value 2"};
// 启动本地通知
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey] != nil){
UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
[self application:application didReceiveLocalNotification:notification];
} else {
//第一次运行
[application setApplicationIconBadgeNumber:0];
[self scheduleLocalNotification];
}
return YES;
}
//收到本地通知 -UIApplicationDelegate
- (void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
NSString *key1Value = notification.userInfo[@"Key 1"];
NSString *key2Value = notification.userInfo[@"Key 2"];
if ([key1Value length] > 0 && [key2Value length] > 0){
NSString *alertString = @"I have receive Local Notification";
application.applicationIconBadgeNumber -= 1; //减少应用图标上的数量
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"local Notification"
message:alertString
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
当接收到本地通知时的各种环境状况:
1。应用已经在前台打开-此时直接调用 application:didReceiveLocalNotification:
2。应用进入后台运行-单击顶端出现的本地通知,之后调用application:didReceiveLocalNotification:
3。app还未开启运行或根本未激活(如双击home键,将app向上拖出,此时app处于未运行状态)
-会先调用application:didFinishLaunchingWithOptions:方法
-此方法中的参数launchOptions字典集中的key:[UIApplicationLaunchOptionsLocalNotificationKey]包含本地通知
-获取到该通知后,需手动调用application:didReceiveLocalNotification:方法
4。iphone设备被锁住-在解锁后,用前3种方法判定
- Scheduling Local Notifications(调度本地通知)
- notifications 消息通知(本地事件通知)
- iOS推送本地通知 Push Notifications: local part
- 【APNs】Scheduling the Delivery of Local Notifications
- 通知机制 (Notifications)
- Notifications(通知)
- Notifications(通知)
- Notifications (通知、通告)
- cordova-plugin-local-notifications本地通知推送在cordova 5.0.0以上版本编译报错解决办法
- Local Notifications
- local notifications
- 浏览器桌面通知(notifications)
- iOS Local Nitification(本地通知)
- 通知(Notifications)
- Notifications 通知
- cordova-plugin-local-notifications发送Android本地消息
- 调度域(Scheduling Domain)
- 任务调度(Task Scheduling)
- Xamarin for Android/IOS离线安装说明
- pd 先留着
- 数据库表数据和表结构复制
- Ubuntu下VIM设置高亮、行号、缩进
- 8. redhat 虚拟ip
- Scheduling Local Notifications(调度本地通知)
- POJ 1305 Fermat vs. Pythagoras 解原毕达哥拉斯三元组
- VS移除所有空行
- iOS开发经验总结
- OCP-1Z0-053-V13.02-111题
- 交叉编绎OpenCv之预览,拍照和录像
- CCControlButton的用法
- easyui 组件的适应
- Android Fragment应用实战,使用碎片向ActivityGroup说再见