推送通知-didFinishLaunchingWithOptions

cds*_*oft 14 cocoa-touch storyboard push-notification ios

当我发送推送通知并且我的应用程序处于打开状态或后台并且我点击推送通知时,我的应用程序重定向到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];
}
Run Code Online (Sandbox Code Playgroud)

上面的代码/方案没有问题,但如果应用程序关闭并且我点击推送通知,则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;
}
Run Code Online (Sandbox Code Playgroud)

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

sta*_*Man 19

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

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

假设,MainMenuViewControllerrootViewController你的navigationController.
设置这个类来听一个NSNotification:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

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

在您的情况下,AppDelegate.m方法应如下所示:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //firstly, don't sleep the thread, it's pointless
    //sleep(1); //remove this line

    if (launchOptions) { //launchOptions is not nil
        NSDictionary *userInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];

        if (apsInfo) { //apsInfo is not nil
            [self performSelector:@selector(postNotificationToPresentPushMessagesVC) 
                       withObject:nil
                       afterDelay:1];
        }
    }
    return YES;
}

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    //this method can be done using the notification as well
    [self postNotificationToPresentPushMessagesVC];
}

-(void)postNotificationToPresentPushMessagesVC {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"HAS_PUSH_NOTIFICATION" object:nil];
}
Run Code Online (Sandbox Code Playgroud)

PS:我还没有为我的项目做过这个(但是)它有效并且是我能想到做这种事情的最佳方式(目前)


Har*_*kar 9

斯威夫特 5

当应用程序被终止并收到推送通知并用户单击它时,在didFinishLaunchingWithOptions 中获取推送通知字典

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    if let userInfo = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [String: AnyObject] {
        if let aps1 = userInfo["aps"] as? NSDictionary {
            print(aps1)
        }
    }
    return true
}
Run Code Online (Sandbox Code Playgroud)

推送通知字典将显示在警报中。


Vla*_*iak 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
Run Code Online (Sandbox Code Playgroud)

}