8

Ich erhalte eine Remote-Benachrichtigung und ändere entsprechend der Art der Benachrichtigung die View-Controller des Navigationscontrollers.Absturz beim Umgang mit Remote-Benachrichtigungen, wenn App nicht ausgeführt wird

Alles funktioniert gut, wenn die App im Vordergrund ist oder wenn die App im Hintergrund, aber nicht vollständig geschlossen ist (von der Multitasking-Leiste aus).

Aber wenn die App geschlossen ist und eine Remote-Benachrichtigung erhält, stürzt sie ab, sobald sie geöffnet wird. Mache ich einen Fehler bei der Einrichtung der ViewControllers?

Hier ist ein Code.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary 
*)launchOptions { 
    // Push required screens into navigation controller 

     UILocalNotification *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]; 

    //Accept push notification when app is not open 
    if (remoteNotif) {  
     [self handleRemoteNotification:application userInfo:remoteNotif.userInfo]; 
     return YES; 
    } 

    [window addSubview:navigationController.view]; 
    [window makeKeyAndVisible]; 

    return YES; 
} 

-(void) handleRemoteNotification:(UIApplication *)application userInfo:(NSDictionary *)userInfo { 
    application.applicationIconBadgeNumber = 0; 

NSMutableArray *viewControllers = [NSMutableArray array]; 
    [viewControllers addObject:driverWaitViewController]; 
    [viewControllers addObject:newJobsViewController]; 

    [navigationController setViewControllers:viewControllers]; 
} 

Antwort

15

habe ich dieses Problem gelöst, und es hat nichts mit View-Controller zu tun, wie ich dachte.

Das Problem war in den folgenden Zeilen. Ich habe remoteNotif.userInfo anstelle von remoteNotif selbst gesendet. Außerdem ist remoteNotif offensichtlich nicht vom Typ UILocalNotification. Es ist ein NSDictionary-Objekt.

Vor

UILocalNotification *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]; 

[self handleRemoteNotification:application userInfo:remoteNotif.userInfo]; 

Sollten

NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]; 

[self handleRemoteNotification:application userInfo:remoteNotif]; 
2

Sie initialisieren nicht richtig Ihre Anwendung, wenn eine Benachrichtigung erhalten. die Anwendung ändern: didFinishLaunchingWithOptions: Methode dazu:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions (NSDictionary *)launchOptions { 
    // Push required screens into navigation controller 

    NSDictionary *notif= [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]; 

    [window addSubview:navigationController.view]; 
    [window makeKeyAndVisible]; 

    //Accept push notification when app is not open 
    if (notif) {  
     [self handleRemoteNotification:application userInfo:notif]; 
    } 

    return YES; 
} 
+0

Danke für Ihre Antwort Vakio. Das Problem ist etwas anderes. Bitte schau, wie ich es gelöst habe. – Prasanna

+0

Ja, ich dachte, das wäre komisch, aber ich habe es nicht verstanden. Es tut uns leid. – vakio

7

wenn Sie die App zu schließen, die von Xcode Debug-Modus zu starten, und wenn die App mit Push-Benachrichtigung (geschlossene app) gestartet werden, wenn Wenn dein Handy mit Mac verbunden ist (immer noch dein Handy im Debug-Modus mit xcode) wird es abstürzen. Testen Sie dieses Senario mit dem unplugged Telefon.

+0

Das hat mein Problem vorerst gelöst, aber warum? Macht keinen Sinn für mich – Evils

+0

Wie Sie sagten, kein Sinn für mich auch. aber ich denke eine Verbindung hier mit dem Gerät und Mac. – damithH

+0

Das hat für mich funktioniert! Macht absolut keinen Sinn. –

Verwandte Themen