2013-05-29 58 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;