Run function when APN is received

耗尽温柔 提交于 2020-01-06 16:19:56

问题


I was able to integrate APNS in my app. Now I want to handle the notifications when the user taps it or when the user receives a notification while using the app. I use the code below to show an alert dialog when a notification is received:

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

Is it possible to run a function in my MainViewController instead of showing the dialog? I tried calling my function MainViewController.refreshData() but it always gives me a nil.


回答1:


Yes it is possible by Posting and adding observer.

Use NSNotificationCenter to make it.

I am posting in Objective C code, try to convert to Swift.

@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;
 }

then paste this code in didRecieveRemoteNotification

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

And in your 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];

  }
 }



回答2:


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


来源:https://stackoverflow.com/questions/37704227/run-function-when-apn-is-received

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!