Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 当应用程序处于终止模式时,如何从推送通知重定向到特定视图控制器_Ios_Objective C_Iphone_Xcode - Fatal编程技术网

Ios 当应用程序处于终止模式时,如何从推送通知重定向到特定视图控制器

Ios 当应用程序处于终止模式时,如何从推送通知重定向到特定视图控制器,ios,objective-c,iphone,xcode,Ios,Objective C,Iphone,Xcode,我需要将应用程序从收到的推送通知重定向到特定的视图控制器。但是,我无法获得将调用哪个委托方法。请导游 下面是我迄今为止尝试过的代码。我尝试过使用选项完成启动,但不起作用 if (launchOptions) { //launchOptions is not nil NSDictionary *userInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; NSDict

我需要将应用程序从收到的推送通知重定向到特定的视图控制器。但是,我无法获得将调用哪个委托方法。请导游

下面是我迄今为止尝试过的代码。我尝试过使用选项完成启动,但不起作用

 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(notificationClickedInNotificationPanel:)
                   withObject:userInfo
                   afterDelay:1];
    }
}
以及

以及


您应该在didFinishLaunchingWithOptions和DidReceivereMotenofication中都这样做,它们在不同的时间被调用。单击通知时,当应用程序完全关闭时调用第一个,当应用程序打开并正在使用时调用第二个

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if (launchOptions != nil)
    {
        NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (dictionary != nil)
        {

            self.animal_id = [dictionary objectForKey:@"animal_id"];
            self.notificationText = [dictionary objectForKey:@"alert"];
            self.soundFile = [dictionary objectForKey:@"sound"];

            if ([self.animal_id length] > 0) {

                NSInteger numberOfBadges = [UIApplication sharedApplication].applicationIconBadgeNumber;
                numberOfBadges -=1;

                if (numberOfBadges < 0) {
                    numberOfBadges = 0;
                }

                [[UIApplication sharedApplication] setApplicationIconBadgeNumber:numberOfBadges];

                doNotShowAlert = YES;
                [self showPetDetails:self.animal_id];
            } else {
                doNotShowAlert = NO;
            }

        }

    }

    return YES;
}

您应该在didFinishLaunchingWithOptions和DidReceivereMotenofication中都这样做,它们在不同的时间被调用。单击通知时,当应用程序完全关闭时调用第一个,当应用程序打开并正在使用时调用第二个

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if (launchOptions != nil)
    {
        NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (dictionary != nil)
        {

            self.animal_id = [dictionary objectForKey:@"animal_id"];
            self.notificationText = [dictionary objectForKey:@"alert"];
            self.soundFile = [dictionary objectForKey:@"sound"];

            if ([self.animal_id length] > 0) {

                NSInteger numberOfBadges = [UIApplication sharedApplication].applicationIconBadgeNumber;
                numberOfBadges -=1;

                if (numberOfBadges < 0) {
                    numberOfBadges = 0;
                }

                [[UIApplication sharedApplication] setApplicationIconBadgeNumber:numberOfBadges];

                doNotShowAlert = YES;
                [self showPetDetails:self.animal_id];
            } else {
                doNotShowAlert = NO;
            }

        }

    }

    return YES;
}

我最近做了一些事情,但是它在
Swift
中,但是如果您想在OC中这样做,它应该非常类似

提示:
didReceiveMemotentification
如果您的应用程序终止,将不会调用,唯一调用的方法是
didFinishLaunchingWithOptions

didfishlaunchingwithoptions
中,您可以执行以下操作

if let launchOpts = launchOptions as [UIApplication.LaunchOptionsKey: Any]? {
            if let notificationPayload = launchOpts[UIApplication.LaunchOptionsKey.remoteNotification] as? NSDictionary {

                 let apsBody = notificationPayload["aps"] as? NSDictionary
                 if(apsBody != nil){
                 // here you can either read value from the `apsBody` dictionary 
                //OR just push the respective controller you want to push
                 }
            }
        }else{
           //go with the regular flow
        }
当你的应用程序被终止时,你没有导航控制器的实例,因此你可能需要确保你有一个导航控制器的实例来推送你的视图


希望这有帮助

我最近做了一些事情,但它在
Swift
中,但如果您想在OC中这样做,它应该非常类似

提示:
didReceiveMemotentification
如果您的应用程序终止,将不会调用,唯一调用的方法是
didFinishLaunchingWithOptions

didfishlaunchingwithoptions
中,您可以执行以下操作

if let launchOpts = launchOptions as [UIApplication.LaunchOptionsKey: Any]? {
            if let notificationPayload = launchOpts[UIApplication.LaunchOptionsKey.remoteNotification] as? NSDictionary {

                 let apsBody = notificationPayload["aps"] as? NSDictionary
                 if(apsBody != nil){
                 // here you can either read value from the `apsBody` dictionary 
                //OR just push the respective controller you want to push
                 }
            }
        }else{
           //go with the regular flow
        }
当你的应用程序被终止时,你没有导航控制器的实例,因此你可能需要确保你有一个导航控制器的实例来推送你的视图


希望这有助于

当应用程序终止时,您可以从
启动选项中找到推送通知负载

以下是更多信息的参考:

当应用程序终止时,您可以从
启动选项中找到推送通知有效负载

以下是更多信息的参考:

objective c仅适用于两个应用程序,它们都实现了代码,但不起作用,甚至无法检查它们是否被调用。要检查它们是否被调用,只需转到应用程序目标->编辑方案->等待可执行文件启动即可。在这些方法中添加断点,并单击您的通知何时启动应用程序。应用程序关闭,然后推送接收,然后点击通知面板。那么,什么时候启动应用程序。首先,请让我知道,当应用程序关闭时,是否可以从通知点击将应用程序内重定向到任何屏幕?目标c仅请在两个应用程序中都实现了代码,但不起作用,甚至无法检查它们是否被调用。它们被调用,要选中,只需转到应用程序目标->编辑方案->等待可执行文件启动即可。在这些方法中添加断点,并单击您的通知何时启动应用程序。应用程序关闭,然后推送接收,然后点击通知面板。那么,什么时候启动应用程序。首先,请让我知道,当应用程序关闭时,是否可以从通知点击将应用程序内重定向到任何屏幕?@Rajesh replace-with?看起来不一样吗?@Rajesh replace-with?看起来不一样吗?
PetDetailViewController *notificationController = [self.rootNavigationController.storyboard instantiateViewControllerWithIdentifier:@"petdetail"];
notificationController.petDetail = petDetail;
notificationController.currentImage = nil;

[self.rootNavigationController pushViewController:notificationController animated:YES];
if let launchOpts = launchOptions as [UIApplication.LaunchOptionsKey: Any]? {
            if let notificationPayload = launchOpts[UIApplication.LaunchOptionsKey.remoteNotification] as? NSDictionary {

                 let apsBody = notificationPayload["aps"] as? NSDictionary
                 if(apsBody != nil){
                 // here you can either read value from the `apsBody` dictionary 
                //OR just push the respective controller you want to push
                 }
            }
        }else{
           //go with the regular flow
        }