2016-09-26 3 views
0

Mein Problem ist, wenn App im Hintergrund ist und Benachrichtigung ankommt und ich öffnete die App von Symbol; App stellt es wieder her, aber ich möchte die Bildschirmdaten in diesem Fall aktualisieren. Gibt es eine Möglichkeit, die Daten im Hintergrund zu aktualisieren, wenn die Benachrichtigung eintrifft? HieriOS: Wenn App im Hintergrund ist und lokale Benachrichtigung angekommen ist; Welche Methode wird automatisch aufgerufen?

ist der Code, den ich für die Bekämpfung der diesen Fall bin mit:

ViewController.m Datei Code:

- (void)viewDidLoad { 
     [super viewDidLoad]; 
     [[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(appIsComingFromBackground:) 
                name:UIApplicationDidBecomeActiveNotification 
                object:nil]; 
     // Do any additional setup after loading the view, typically from a nib. 
    } 
    - (void) appIsComingFromBackground:(NSNotification *) note { 
     // code 
     NSString *hasMessage = [[NSUserDefaults standardUserDefaults] objectForKey:@"alertmsg"]; 
     if([hasMessage length]!=0) 
     { 
      _labelText.text = hasMessage; 
      [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"alertmsg"]; 
     } 
     else{ 
      _labelText.text = @""; 
     } 
    } 

AppDelegate.m Datei Code:

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

    if (application.applicationState == UIApplicationStateActive) { 

    } 
    else if(application.applicationState == UIApplicationStateBackground || application.applicationState == UIApplicationStateInactive) 
    { 
     [[NSUserDefaults standardUserDefaults] setObject:notification.alertTitle forKey:@"alertmsg"]; 
    } 
    NSLog(@"Alert Message: %@", notification.alertTitle); 
    NSLog(@"Alert Body: %@", notification.alertBody); 
} 
+1

Wenn die App dann wird keine AppDelegate suspendiert Methode aufgerufen, wenn eine lokale Benachrichtigung angezeigt wird. – Paulw11

+0

@ Paulw11 also gibt es keine Lösung für dieses Problem? –

+0

Gibt es eine Möglichkeit, Daten zu aktualisieren, wenn der Benutzer die App nicht von der Benachrichtigung aus geöffnet hat, sondern vom Symbol? –

Antwort

3

Anwendung ist NICHT Laufen

Wenn die App nicht läuft, siehe Benutzer-Benachrichtigungen auf folgende Weise in Abhängigkeit von den Benachrichtigungseinstellungen:

  • Mail oder Banner

  • Anzeige der App-Symbol

  • Badging Abspielen einer Sound

Durch Antippen der Aktionsschaltfläche der Benachrichtigung starten Benutzer die App. In diesem Fall wird die Anwendung: didFinishLaunchingWithOptions: Methode des Anwendungsdelegaten aufgerufen.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
// Override point for customization after application launch. 

// Handle launching from a notification 
UILocalNotification *locationNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; 
if (locationNotification) { 
    // Set icon badge number to zero 
    application.applicationIconBadgeNumber = 0; 
} 

return YES; 
} 

Applicaton im Vordergrund läuft

Wenn der App beim Laufen wird die Meldung abgegeben wird, gibt es keine Warnmeldung auf dem Bildschirm angezeigt. Die Anwendung ruft automatisch die Anwendung ihres Delegierten auf: didReceiveLocalNotification: method.

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
{ 
UIApplicationState state = [application applicationState]; 
if (state == UIApplicationStateActive) { 

} 

// Request to reload table view data 
[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self]; 

// Set icon badge number to zero 
application.applicationIconBadgeNumber = 0; 
} 

Anwendung im Hintergrund läuft

Die App vor ins Leben gerufen wurden, aber der Benutzer wechseln zu einer anderen App. Wenn die Benachrichtigung ausgelöst wird, wird Benutzern normalerweise oben auf dem Bildschirm ein Warnbanner angezeigt. Wenn es angetippt wird, wird die App in den Vordergrund gebracht. Ähnlich wie im Fall, dass die App im Vordergrund ausgeführt wird, ruft die Anwendung automatisch die Anwendung des Delegate auf: didReceiveLocalNotification: method.

+0

Ja, Sie haben Recht, aber Problem ist, wenn Benutzer App von Symbol als' Anwendung öffnet: didReceiveLocalNotification' wird nicht aufgerufen und Daten werden auch in diesem Fall nicht aktualisiert . 'application: didReceiveLocalNotification' wird nur aufgerufen, wenn der Benutzer die App über Banner oder Benachrichtigung öffnet. –

+0

@MuhammadUmair - siehe die paul Befehl –

+0

sehen diese https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html –

-1
In Appdelegate 

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

if (application.applicationState == UIApplicationStateActive) { 

} 
else if(application.applicationState == UIApplicationStateBackground || application.applicationState == UIApplicationStateInactive) 
{ 
    // [[NSUserDefaults standardUserDefaults] setObject:notification.alertTitle forKey:@"alertmsg"]; 

[[NSNotificationCenter defaultCenter]postNotificationName:@"PushNotificationReceived" object:nil userInfo:userInfo]; 

} 
NSLog(@"Alert Message: %@", notification.alertTitle); 
NSLog(@"Alert Body: %@", notification.alertBody); 

} 


In view controller: 

-(void)viewDidLoad 
{ 
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(doChangesWhatdidYouWant:) name:@"PushNotificationReceived" object:nil]; 
} 

-(void)doChangesWhatdidYouWant:(NSNotification *)notification{ 
//Todo 
} 
Verwandte Themen