2013-05-29 15 views
10

Qualcuno può dirmi come ottenere un UILocalNotification mentre la mia app è in background.Notifica locale in background

Sto postando il mio codice qui. Grazie in anticipo.

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

Cosa intendi per "ricevere notifiche locali mentre l'app è in background"? Se pianifichi una notifica locale e questa viene attivata mentre la tua app è in background, l'utente vedrà una vista di avviso ma il gioco è fatto. Non puoi ricevere un messaggio che ti informa che la notifica locale è stata attivata! – Dabrut

+0

BUt nel mio caso non viene licenziato. non so perché? –

+0

Quale è il valore di startDate? Prova [NSDate dateWithTimeIntervalSinceNow: 60] e rimuovendo il fuso orario – Dabrut

risposta

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) 
    { 
    } 
} 

Spero che questo vi aiuterà a :)

+1

Grazie mille, ha funzionato b :) –

+1

Ur Welcome .. :) – Abha

+3

Per iOS 8 o versione successiva, è necessario chiedere anche il permesso dell'utente: if ([[[UIDevice currentDevice] systemVersion] floatValue]> = 8.0) { [[UIApplication sharedApplication] registerUserNotificationSettings: [UIUserNotificationSettings settingsForTypes: (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categorie: nil]]; } –

1

addTimerInterval è depcrecated in iOS 4.0, assicuratevi di destinazione di distribuzione. Puoi usare.

- (id)dateByAddingTimeInterval:(NSTimeInterval)ti 

anche assicurarsi di impostare il valore corretto per fireDate

1

Si prega di utilizzare il codice.

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; 
Problemi correlati