2016-06-08 8 views
0

Ich konnte APNS in meiner App integrieren. Jetzt möchte ich die Benachrichtigungen behandeln, wenn der Benutzer darauf klickt oder wenn der Benutzer eine Benachrichtigung erhält, während er die App verwendet. Ich verwende den folgenden Code einen Warnhinweis angezeigt wird, wenn eine Benachrichtigung empfangen wird:Run-Funktion, wenn APN empfangen wird

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 
     // display the userInfo 
     if let notification = userInfo["aps"] as? NSDictionary, 
      let alert = notification["alert"] as? String { 
      var alertCtrl = UIAlertController(title: "Time Entry", message: alert as String, preferredStyle: UIAlertControllerStyle.Alert) 
      alertCtrl.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) 
      // Find the presented VC... 
      var presentedVC = self.window?.rootViewController 
      while (presentedVC!.presentedViewController != nil) { 
       presentedVC = presentedVC!.presentedViewController 
      } 
      presentedVC!.presentViewController(alertCtrl, animated: true, completion: nil) 

      // call the completion handler 
      // -- pass in NoData, since no new data was fetched from the server. 
      completionHandler(UIBackgroundFetchResult.NoData) 
     } 
    } 

Ist es möglich, eine Funktion in meinem Mainviewcontroller anstatt zu zeigen, um den Dialog zu führen? Ich habe versucht, meine Funktion MainViewController.refreshData() anzurufen, aber es gibt mir immer eine nil.

Antwort

1

Ja, es ist möglich, indem Sie Beobachter hinzufügen und hinzufügen.

Verwenden Sie NSNotificationCenter, um es zu erstellen.

Ich poste in Objective C-Code, versuche, zu Swift zu konvertieren.

@implementation AppDelegate 

static void displayStatusChanged(CFNotificationCenterRef center, void *observer, 
          CFStringRef name, const void *object, 
          CFDictionaryRef userInfo) { 
if (name == CFSTR("com.apple.springboard.lockcomplete")) { 
    [[NSUserDefaults standardUserDefaults] setBool:YES 
              forKey:@"kDisplayStatusLocked"]; 
    [[NSUserDefaults standardUserDefaults] synchronize]; 
} 
} 


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
// Override point for customization after application launch. 
CFNotificationCenterAddObserver(
           CFNotificationCenterGetDarwinNotifyCenter(), NULL, displayStatusChanged, 
           CFSTR("com.apple.springboard.lockcomplete"), NULL, 
           CFNotificationSuspensionBehaviorDeliverImmediately); 
return YES; 
} 

dann fügen Sie diesen Code in didRecieveRemoteNotification

[[NSNotificationCenter defaultCenter]postNotificationName:@"TestNotification" object:self]; 

Und in Ihrem Mainviewcontroller

- (void)viewDidLoad { 

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(receiveTestNotification:) 
              name:@"TestNotification" 
              object:nil]; 


    } 


- (void) receiveTestNotification:(NSNotification *) notification 
{ 

if ([[notification name] isEqualToString:@"TestNotification"]) { 
    NSLog (@"Successfully received the test notification!"); 

    //perform your operations 
    [self refreshData]; 

    } 
} 
+0

Entschuldigung, ich kann das nicht in Swift konvertieren. Ich habe keinen Hintergrund in C und ich fing an, ios dev mit swift bereits zu lernen. –

+0

verwenden Sie einfach diese Methoden, es wird Ihnen automatische Vorschläge geben, keine Notwendigkeit, sich viel zu sorgen. Es wird dein Problem lösen. – Santo

+0

Danke! Ich habe es gemacht! Es war einfach! –

0
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 
    // display the userInfo 
    if let notification = userInfo["aps"] as? NSDictionary, 
     let alert = notification["alert"] as? String { 
      var alertCtrl = UIAlertController(title: "Time Entry", message: alert as String, preferredStyle: UIAlertControllerStyle.Alert) 
      alertCtrl.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (UIAlertAction) -> Void in 
          //Your Function call when you click Ok Button. 
          self.YourFunc() 
          })) 
      // Find the presented VC... 
      var presentedVC = self.window?.rootViewController 
      while (presentedVC!.presentedViewController != nil) { 
       presentedVC = presentedVC!.presentedViewController 
      } 
      presentedVC!.presentViewController(alertCtrl, animated: true, completion: nil) 

      // call the completion handler 
      // -- pass in NoData, since no new data was fetched from the server. 
      completionHandler(UIBackgroundFetchResult.NoData) 
    } 
} 
+0

Wo meine Funktion gibt es? –

Verwandte Themen