Apr 26
Iniziamo a vedere cosa ci offre il nuovo firmware 4.0 di iPhone.
Create un progetto come applicazione Window-based e chiamatela LocalPush.
Aggiungete l’stanza di bgTask in LocalPushAppDelegate:
@interface LocalPushAppDelegate : NSObject { UIWindow *window; UIBackgroundTaskIdentifier bgTask; }
In LocalPushAppDelegate.m avremo:
// // LocalPushAppDelegate.m // LocalPush // @interface ToDoItem : NSObject { NSInteger year; NSInteger month; NSInteger day; NSInteger hour; NSInteger minute; NSInteger second; NSString *eventName; } @property (nonatomic, readwrite) NSInteger year; @property (nonatomic, readwrite) NSInteger month; @property (nonatomic, readwrite) NSInteger day; @property (nonatomic, readwrite) NSInteger hour; @property (nonatomic, readwrite) NSInteger minute; @property (nonatomic, readwrite) NSInteger second; @property (nonatomic, copy) NSString *eventName; @end @implementation ToDoItem @synthesize year, month, day, hour, minute, second, eventName; @end #import "LocalPushAppDelegate.h" @implementation LocalPushAppDelegate @synthesize window; #define ToDoItemKey @"EVENTKEY1" #define MessageTitleKey @"MSGKEY1" - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // avvio dell'applicazione UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; if (localNotif) { NSString *itemName = [localNotif.userInfo objectForKey:ToDoItemKey]; // [viewController displayItem:itemName]; // custom method application.applicationIconBadgeNumber = localNotif.applicationIconBadgeNumber-1; NSLog(@"E' localNotif %@",itemName); } else { [[UIApplication sharedApplication] cancelAllLocalNotifications]; NSDate *now = [NSDate date]; NSLog(@"ora è %@",now); NSDate *scheduled = [now dateByAddingTimeInterval:120] ; NSCalendar *calendar = [NSCalendar currentCalendar]; unsigned int unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit; NSDateComponents *comp = [calendar components:unitFlags fromDate:scheduled]; NSLog(@"schedulato %@",scheduled); ToDoItem *todoitem = [[ToDoItem alloc] init]; todoitem.day = [comp day]; todoitem.month = [comp month]; todoitem.year = [comp year]; todoitem.hour = [comp hour]; todoitem.minute = [comp minute]; todoitem.eventName = @"Testing Event"; [self scheduleNotificationWithItem:todoitem interval:1]; NSLog(@"scheduleNotificationWithItem"); } [window makeKeyAndVisible]; return YES; } - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notif { NSLog(@"application: didReceiveLocalNotification:"); NSString *itemName = [notif.userInfo objectForKey:ToDoItemKey]; NSString *messageTitle = [notif.userInfo objectForKey:MessageTitleKey]; // [viewController displayItem:itemName]; // custom method [self _showAlert:itemName withTitle:messageTitle]; NSLog(@"Riceve Local Notification mentre l'app è ancora in esecuzione..."); NSLog(@"notification corrente %@",notif); application.applicationIconBadgeNumber = notif.applicationIconBadgeNumber-1; } - (void) _showAlert:(NSString*)pushmessage withTitle:(NSString*)title { UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title message:pushmessage delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; if (alertView) { [alertView release]; } } - (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 dateByAddingTimeInterval:-(minutesBefore*60)]; NSLog(@"Data %@",localNotif.fireDate); localNotif.timeZone = [NSTimeZone defaultTimeZone]; localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"%@ in %i minuti.", nil), item.eventName, minutesBefore]; localNotif.alertAction = NSLocalizedString(@"View Details", nil); localNotif.soundName = UILocalNotificationDefaultSoundName; localNotif.applicationIconBadgeNumber = 1; // NSDictionary *infoDict = [NSDictionary dictionaryWithObject:item.eventName forKey:ToDoItemKey]; NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:item.eventName,ToDoItemKey, @"Local Push ricevuto mentre è in esecuzione", MessageTitleKey, nil]; localNotif.userInfo = infoDict; [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; NSLog(@"scheduledLocalNotifications sono %@", [[UIApplication sharedApplication] scheduledLocalNotifications]); [localNotif release]; } - (NSString *) checkForIncomingChat { return @"Soft!"; }; - (void)applicationDidEnterBackground:(UIApplication *)application { NSLog(@"L'applicazione entra in un stato di background."); // UIBackgroundTaskIdentifier bgTask istanza 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(@"%@ ha un messaggio per te.", nil), friend]; localNotif.alertAction = NSLocalizedString(@"Read Msg", nil); localNotif.soundName = @"alarmsound.caf"; localNotif.applicationIconBadgeNumber = 1; NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Tuo task in background",ToDoItemKey, @"Message from Soft!", MessageTitleKey, nil]; localNotif.userInfo = infoDict; [application presentLocalNotificationNow:localNotif]; [localNotif release]; friend = nil; break; } } } [application endBackgroundTask:self->bgTask]; self->bgTask = UIInvalidBackgroundTask; }); } - (void)dealloc { [window release]; [super dealloc]; } @end
Ultimi Commenti