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

时间:2019-06-27 13:05:15

标签: ios objective-c iphone xcode

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

下面是我到目前为止尝试过的代码。我已经在didFinishLaunchingWithOptions中尝试过,但是没有用。

import SwiftUI

struct ContentView : View {
    init() {
        UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.red]
        UINavigationBar.appearance().backgroundColor = .green
    }
    var body: some View {

        NavigationView {
            NavigationButton(destination: SecondPage(), label: {
                Text("Click")
            })
            .navigationBarTitle(Text("Title"), displayMode: .inline)
        }
    }
}

4 个答案:

答案 0 :(得分:1)

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

// Called when App is in Foreground // Using the data received in the notification you can call the desired view controller

    completionHandler([.alert, .badge, .sound])
}

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

// Called when App is not in  Foreground // Using the data received in the notification you can call the desired view controller
    completionHandler()
}

答案 1 :(得分:1)

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

提示::如果您的应用终止,则不会调用didReceiveRemoteNotification,唯一调用的方法是didFinishLaunchingWithOptions

didFinishLaunchingWithOptions内,您可以执行以下操作

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
        }

在您的应用终止时要知道这一点,您没有navigationController的实例,因此您可能要确保有一个导航控制器的实例来推送视图。

希望有帮助

答案 2 :(得分:0)

您应该同时在didFinishLaunchingWithOptions和didReceiveRemoteNotification中进行操作,它们在不同的时间被调用。单击通知时,当应用程序完全关闭时,第一个调用;当应用程序打开并使用时,第二个调用。

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

在这里:

-(void)application:(UIApplication *)app didReceiveRemoteNotification:(NSDictionary *)userInfo
{

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

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

    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:numberOfBadges];

    self.animal_id  = [userInfo objectForKey:@"animal_id"];

    NSDictionary *aps = [userInfo objectForKey:@"aps"];

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

   [self showPetDetails:self.animal_id];
}

showPetDetails进入数据库以获取要显示的详细信息。拥有时,它调用另一个方法来显示详细信息视图:

PetDetailViewController *notificationController = [self.rootNavigationController.storyboard instantiateViewControllerWithIdentifier:@"petdetail"];
notificationController.petDetail = petDetail;
notificationController.currentImage = nil;

[self.rootNavigationController pushViewController:notificationController animated:YES];

答案 3 :(得分:0)

应用被杀死后,您可以从super.execute()

中找到推送通知有效内容

此处是更多信息的参考: https://developer.apple.com/documentation/uikit/uiapplication/launchoptionskey/1622967-remotenotification