推送通知-didFinishLaunchingWithOptions

时间:2013-12-25 09:39:32

标签: ios cocoa-touch storyboard push-notification

当我发送推送通知并且我的应用程序处于打开状态或后台并点击推送通知时,我的应用程序会重定向到PushMessagesVc viewController按预期

我使用下面的代码:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    PushMessagesVc *pvc = [mainstoryboard instantiateViewControllerWithIdentifier:@"PushMessagesVc"];

    [self.window.rootViewController presentViewController:pvc
                                                 animated:YES
                                               completion:NULL];
}

上面的代码/方案没有问题,但是如果应用程序已关闭并且我点击了推送通知,那么在这种情况下,应用程序不会重定向PushMessagesVc viewController&应用程序保留在主屏幕上。

对于第二种情况,我使用以下代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    sleep(1);
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeNone)];
    [UIApplication sharedApplication].applicationIconBadgeNumber = 1;

    NSDictionary *userInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
    NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];

    if(apsInfo) {
        UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        PushMessagesVc* pvc = [mainstoryboard instantiateViewControllerWithIdentifier:@"PushMessagesVc"];
        [self.window.rootViewController presentViewController:pvc animated:YES completion:NULL];
        return YES;
    }
    return YES;
}

但在这种情况下,PushMessagesVc不会出现。

5 个答案:

答案 0 :(得分:19)

由于您只想在获得推送通知时提供viewController,因此您可以尝试将NSNotificationCenter用于您的目的:

第1部分:设置一个类(在您的情况下为rootViewController )来监听/回复NSNotification

假设 MainMenuViewController rootViewController的{​​{1}}。
设置此课程以收听navigationController

NSNotification

第2部分:发布通知(可能来自代码中的任何位置

在您的情况下, AppDelegate.m 方法应该是

- (void)viewDidLoad {
    //...
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(presentMyViewOnPushNotification)
                                                 name:@"HAS_PUSH_NOTIFICATION"
                                               object:nil];
}

-(void)presentMyViewOnPushNotification {
    //The following code is no longer in AppDelegate
    //it should be in the rootViewController class (or wherever you want)

    UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    PushMessagesVc *pvc = [mainstoryboard instantiateViewControllerWithIdentifier:@"PushMessagesVc"];

    [self presentViewController:pvc animated:YES completion:nil];
    //either presentViewController (above) or pushViewController (below)
    //[self.navigationController pushViewController:pvc animated:YES];
}

PS:我没有为我的项目(尚未)做到这一点,但它有效并且是我想到做这种事情的最佳方式(目前 >)

答案 1 :(得分:5)

Swift 2.0 对于“未运行”状态(本地和远程通知)

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {


// Handle notification
if (launchOptions != nil) {

    // For local Notification
    if let localNotificationInfo = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification {

        if let something = localNotificationInfo.userInfo!["yourKey"] as? String {

            self.window!.rootViewController = UINavigationController(rootViewController: YourController(yourMember: something))
        }


    } else

    // For remote Notification
    if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as! [NSObject : AnyObject]? {

        if let something = remoteNotification["yourKey"] as? String {
            self.window!.rootViewController = UINavigationController(rootViewController: YourController(yourMember: something))
        }
    }

}


return true

}

答案 2 :(得分:3)

Swift 3 当应用程序被杀并且推送通知接收并且用户点击该

时,在 didFinishLaunchingWithOptions 中获取推送通知字典
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    if let userInfo = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String: AnyObject] {
        if let aps1 = userInfo["aps"] as? NSDictionary {
            print(aps1)
        }
    }
    return true
}

推送通知字典将以警告显示。

答案 3 :(得分:1)

快捷键5

我在didFinishLaunchingWithOptions中将通知写入var“ notification”,如果“ notification”!= nil开始在applicationDidBecomeActive中推送,之后将其删除(= nil)

class AppDelegate: UIResponder, UIApplicationDelegate {

var notification: [AnyHashable : Any]? = nil

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    registerForPushNotifications()

    //write notification to var
    if launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] != nil {
        self.notification = launchOptions![UIApplication.LaunchOptionsKey.remoteNotification] as? [AnyHashable : Any]
    }

    return true
}

func applicationDidBecomeActive(_ application: UIApplication) {
    if notification != nil {
    //for launch notification after terminate app
        NotificationCenter.default.post(name: Notification.Name(rawValue: "startPush"), object: nil)
        notification = nil
    }
}

答案 4 :(得分:0)

Swift版本:

   if let localNotification: UILocalNotification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification { //launchOptions is not nil
        self.application(application, didReceiveLocalNotification: localNotification)
    }