再記錄下來了,
今次的是Local Notification
1.即時的 Local Notification
UIBackgroundTaskIdentifier bgTask;
- (void)applicationDidEnterBackground:(UIApplication *)application {
if(bgTask == UIBackgroundTaskInvalid)
{
UIApplication* app = [UIApplication sharedApplication];
// 開啟了BackgroundTask就要以令以下的queue在Background/Foreground Task都可以運行
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"System Expiration End Background Task");
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UILocalNotification* notification = [[UILocalNotification alloc] init];
notification.alertBody = @"Test";
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
[notification release];
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
if(bgTask == UIBackgroundTaskInvalid)
{
UIApplication* app = [UIApplication sharedApplication];
// 開啟了BackgroundTask就要以令以下的queue在Background/Foreground Task都可以運行
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"System Expiration End Background Task");
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UILocalNotification* notification = [[UILocalNotification alloc] init];
notification.alertBody = @"Test";
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
[notification release];
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}
}
2.Schedule Local Notification
UILocalNotification* notification = [[UILocalNotification alloc] init];
notification.alertBody = @"Test";
// 在當前時間10秒後發動
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
// Alert View 不會顯示 View button
notification.hasAction = FALSE;
notification.userInfo = [NSDictionary dictionaryWithObject:@"Key" forKey:@"Key"];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[notification release];
notification.alertBody = @"Test";
// 在當前時間10秒後發動
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
// Alert View 不會顯示 View button
notification.hasAction = FALSE;
notification.userInfo = [NSDictionary dictionaryWithObject:@"Key" forKey:@"Key"];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[notification release];
3.Cancel Local Notification
// 在 scheduled 的 Local Notification Array 找出要的 Local Notification Object
NSUInteger idx = [[UIApplication sharedApplication].scheduledLocalNotifications indexOfObjectPassingTest:
^(id obj, NSUInteger idx, BOOL *stop)
{
NSDictionary* userData = ((UILocalNotification*)obj).userInfo;
NSInteger key = [[userData valueForKey:@"Key"] integerValue];
if([key isEqualToString:@"Key"])
{
*stop = TRUE;
return YES;
}
return NO;
}];
if(idx != NSNotFound)
{
UILocalNotification* notification = [[UIApplication sharedApplication].scheduledLocalNotifications objectAtIndex:idx];
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
NSUInteger idx = [[UIApplication sharedApplication].scheduledLocalNotifications indexOfObjectPassingTest:
^(id obj, NSUInteger idx, BOOL *stop)
{
NSDictionary* userData = ((UILocalNotification*)obj).userInfo;
NSInteger key = [[userData valueForKey:@"Key"] integerValue];
if([key isEqualToString:@"Key"])
{
*stop = TRUE;
return YES;
}
return NO;
}];
if(idx != NSNotFound)
{
UILocalNotification* notification = [[UIApplication sharedApplication].scheduledLocalNotifications objectAtIndex:idx];
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
沒有留言:
張貼留言