本地通知。
来源:互联网 发布:民生证券软件下载 编辑:程序博客网 时间:2024/05/01 21:19
Local and Push Notification Programming Guide
UILocalNotification *notification=[[[UILocalNotificationalloc] init]autorelease];
if (notification!=nil) {
NSDate *now=[NSDatenew];
notification.fireDate=[now dateByAddingTimeInterval:4];
notification.timeZone=[NSTimeZonedefaultTimeZone];
notification.alertBody=@"该去吃晚饭了!";
[[UIApplicationsharedApplication]
scheduleLocalNotification:notification];
}
Scheduling Local Notifications
Creating and scheduling local notifications in iOS requires that you perform a few simple steps:
Allocate and initialize a
UILocalNotification
object.Set the date and time that the operating system should deliver the notification. This is the
fireDate
property.If you set the
timeZone
property to theNSTimeZone
object for the current locale, the system automatically adjusts the fire date when the device travels across (and is reset for) different time zones. (Time zones affect the values of date components—that is, day, month, hour, year, and minute—that the system calculates for a given calendar and date value.) You can also schedule the notification for delivery on a recurring basis (daily, weekly, monthly, and so on).Configure the substance of the notification: alert, icon badge number, and sound.
The alert has a property for the message (the
alertBody
property) and for the title of the action button or slider (alertAction
); both of these string values can be internationalized for the user’s current language preference.You set the badge number to display on the application icon through the
applicationIconBadgeNumber
property.You can assign the filename of a nonlocalized custom sound in the application’s main bundle to the
soundName
property; to get the default system sound, assignUILocalNotificationDefaultSoundName
. Sounds should always accompany an alert message or icon badging; they should not be played otherwise.
Optionally, you can attach custom data to the notification through the
userInfo
property.Keys and values in the
userInfo
dictionary must be property-list objects.Schedule the local notification for delivery.
You schedule a local notification by calling the
UIApplication
methodscheduleLocalNotification:
. The application uses the fire date specified in theUILocalNotification
object for the moment of delivery. Alternatively, you can present the notification immediately by calling thepresentLocalNotificationNow:
method.
The method in Listing 2-1 creates and schedules a notification to inform the user of a hypothetical to-do list application about the impending due date of a to-do item. There are a couple things to note about it. For the alertBody
and alertAction
properties, it fetches from the main bundle (via theNSLocalizedString
macro) strings localized to the user’s preferred language. It also adds the name of the relevant to-do item to a dictionary assigned to the userInfo
property.
Creating, configuring, and scheduling a local notification
- (void)scheduleNotificationWithItem:(ToDoItem *)item interval:(int)minutesBefore {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:item.day];
[dateComps setMonth:item.month];
[dateComps setYear:item.year];
[dateComps setHour:item.hour];
[dateComps setMinute:item.minute];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = [itemDate addTimeInterval:-(minutesBefore*60)];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"%@ in %i minutes.", nil),
item.eventName, minutesBefore];
localNotif.alertAction = NSLocalizedString(@"View Details", nil);
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:item.eventName forKey:ToDoItemKey];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
}
You can cancel a specific scheduled notification by calling cancelLocalNotification:
on the application object, and you can cancel all scheduled notifications by calling cancelAllLocalNotifications
. Both of these methods also programmatically dismiss a currently displayed notification alert.
Applications might also find local notifications useful when they run in the background and some message, data, or other item arrives that might be of interest to the user. In this case, they should present the notification immediately using the UIApplication
method presentLocalNotificationNow:
(iOS gives an application a limited time to run in the background). Listing 2-2 illustrates how you might do this.
Presenting a local notification immediately while running in the background
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"Application entered background state.");
// bgTask is instance variable
NSAssert(self->bgTask == UIInvalidBackgroundTask, nil);
bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
dispatch_async(dispatch_get_main_queue(), ^{
[application endBackgroundTask:self->bgTask];
self->bgTask = UIInvalidBackgroundTask;
});
}];
dispatch_async(dispatch_get_main_queue(), ^{
while ([application backgroundTimeRemaining] > 1.0) {
NSString *friend = [self checkForIncomingChat];
if (friend) {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif) {
localNotif.alertBody = [NSString stringWithFormat:
NSLocalizedString(@"%@ has a message for you.", nil), friend];
localNotif.alertAction = NSLocalizedString(@"Read Message", nil);
localNotif.soundName = @"alarmsound.caf";
localNotif.applicationIconBadgeNumber = 1;
[application presentLocalNotificationNow:localNotif];
[localNotif release];
friend = nil;
break;
}
}
}
[application endBackgroundTask:self->bgTask];
self->bgTask = UIInvalidBackgroundTask;
});
}
- 本地通知
- 本地通知。
- 本地通知
- 本地通知
- 本地通知
- 本地通知
- 本地通知
- 本地通知
- 本地通知
- 本地通知
- 本地通知
- 本地通知
- 本地通知
- 本地通知
- 本地通知
- 本地通知
- 本地通知
- 本地通知
- 在Windows平台上安装Node.js及NPM模块管理
- writing dumpstate to file android
- HTTP Header 详解
- erl_crash.dump 的查看
- SQLite—参数绑定和重执行已编译语句
- 本地通知。
- jQuery设置 select、radio、checkbox 默认选中的值
- 在外面打工的人很可怜
- Android 中Odex文件生成与合并, 移植apk【转载+修正】
- IOS学习六:Date Picker, Picker View选取器控件初步
- mysql 安装
- ftp文件传输协议
- php-Arrays 函数-array_diff_uassoc-用回调函数做索引检查来计算数组的差值
- 现有游戏引擎的分析—— 2D (一)