开发者

How to initialize local notification?

I want to implement local notification in my clock app.Basically i want that a music file should be played after every half an hour like in ship开发者_JS百科's clock in which chimes are played after every 30 minutes.

Can anyone give rough idea as how i can implement this functionality even when the app enters in background?


I recently used the Local notification stuff and used the following functions

//Setting up the Local Notifications

for (int i= 1 ; i<=10; i++) { //We here set 10 Notification after every 30 minutes from now you can modify it accordingly

    NSDate *scheduled = [[NSDate date] dateByAddingTimeInterval:60*30*i]; //These are seconds

    NSDictionary* dataDict = [NSDictionary dictionaryWithObjectsAndKeys:scheduled,FIRE_TIME_KEY,@"Background Notification received",NOTIFICATION_MESSAGE_KEY,nil];

    [self scheduleNotificationWithItem:dataDict];

}

Where scheduleNotificationWithItem is defined as

- (void)scheduleNotificationWithItem:(NSDictionary*)item  {

    UILocalNotification *localNotification = [[UILocalNotification alloc] init];

    if (localNotification == nil)   return;

    localNotification.fireDate =  [item valueForKey:FIRE_TIME_KEY];

    localNotification.timeZone = [NSTimeZone defaultTimeZone];

    localNotification.alertBody = [NSString stringWithFormat:NSLocalizedString(@"%@", nil), [item valueForKey:NOTIFICATION_MESSAGE_KEY]];

    localNotification.alertAction = NSLocalizedString(@"View Details", nil);

    localNotification.soundName = UILocalNotificationDefaultSoundName;

    localNotification.userInfo = item;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

    [localNotification release];
}

Finally you can handle these notifications as

You can handle these notifications as follows

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

// Do the required work you can obtain additional Info via notification.userInfo which happens to be a dictionary

}

reading the developer documentation will help you more to understand the stuff.Hope it helps


You can use UILocalNotifications and set their 'firedate', according to your requirement and then schedule the notification. These notifications doesn't bother whether your app is running or is in background they will always show up like an alertview.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜