2013-05-29 143 views
10

任何人都可以请告诉我如何在我的应用程序处于后台时获取UILocalNotification后台本地通知

我在这里发布我的代码。提前致谢。

UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 
if (localNotif == nil) 
    return; 
localNotif.fireDate =[startDate addTimeInterval:60]; 
NSLog(@"%@",localNotif.fireDate); 

localNotif.timeZone = [NSTimeZone defaultTimeZone];  
localNotif.alertAction = @"View"; 
localNotif.soundName = UILocalNotificationDefaultSoundName; 

NSString *notifStr=[NSString stringWithFormat:@"Hey looks like you're meeting up with %@, why don't you let your other friends know what fun they're missing out on? Share a photo :)",[itemDict objectForKey:@"fname"]]; 

NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:notifStr ,@"notifKey",nil]; 
localNotif.userInfo = infoDict; 

[[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
[localNotif release]; 
+1

你是什么意思“获取本地通知,而应用程序在后台”?如果您安排本地通知,并在应用程序处于后台时触发,用户将看到警报视图,但就是这样。你无法收到一条消息告诉你本地通知被触发! – Dabrut

+0

在我的情况下,它并没有被解雇。我不知道为什么? –

+0

startDate的价值是什么?尝试[NSDate dateWithTimeIntervalSinceNow:60]并删除时区 – Dabrut

回答

20
-(void)insert:(NSDate *)fire 
{ 
    [[UIApplication sharedApplication]cancelAllLocalNotifications]; 
    self.localNotification = [[UILocalNotification alloc] init]; 
    if (self.localNotification == nil) 
    { 
     return; 
    } 
    else 
    { 
     self.localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60]; 
     self.localNotification.alertAction = nil; 
     self.localNotification.soundName = UILocalNotificationDefaultSoundName; 
     self.localNotification.alertBody = @"Hey looks like you're meeting up with %@, why don't you let your other friends know what fun they're missing out on? Share a photo :)"; 
     self.localNotification.alertAction = NSLocalizedString(@"Read Msg", nil); 
     self.localNotification.applicationIconBadgeNumber=1; 
     self.localNotification.repeatInterval=0; 
     [[UIApplication sharedApplication] scheduleLocalNotification:self.localNotification]; 
    } 
} 

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif 
{ 
    [[UIApplication sharedApplication]cancelAllLocalNotifications]; 
    app.applicationIconBadgeNumber = notif.applicationIconBadgeNumber -1; 

    notif.soundName = UILocalNotificationDefaultSoundName; 

    [self _showAlert:[NSString stringWithFormat:@"%@",Your msg withTitle:@"Title"]; 

} 

- (void) _showAlert:(NSString*)pushmessage withTitle:(NSString*)title 
{ 
    [self.alertView_local removeFromSuperview]; 
    self.alertView_local = [[UIAlertView alloc] initWithTitle:title message:pushmessage delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [self.alertView_local show]; 

    if (self.alertView_local) 
    { 
    } 
} 

希望正确的值,这将帮助你:)

+1

非常感谢,它工作b :) –

+1

欢迎光临.. :) – Abha

+3

对于iOS 8或以上版本,您还需要征得用户的许可: if([[[UIDevice currentDevice] systemVersion] floatValue]> = 8.0 ) [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes :(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; } –

1

addTimerInterval在iOS 4.0中被剥夺了,请确保您的部署目标。您可以使用。

- (id)dateByAddingTimeInterval:(NSTimeInterval)ti 

还要确保您为fireDate

1

请使用下面的代码。

self.localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60]; 
    self.localNotification.alertAction = nil; 
    self.localNotification.soundName = UILocalNotificationDefaultSoundName; 
    self.localNotification.alertBody = @"Hey looks like you're meeting up with %@, why don't you let your other friends know what fun they're missing out on? Share a photo :)"; 
    self.localNotification.alertAction = NSLocalizedString(@"Read Msg", nil); 
    self.localNotification.applicationIconBadgeNumber=1; 
    self.localNotification.repeatInterval=0;