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

Posted

技术标签:

【中文标题】当应用程序处于终止模式时,如何从推送通知重定向到特定的视图控制器【英文标题】:How to redirect to particular view controller from push notification when app is in terminated mode 【发布时间】:2019-06-27 13:05:15 【问题描述】:

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

以下是我迄今为止尝试过的代码。我已经尝试过 didFinishLaunchingWithOptions 但不起作用。

 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];
    

【问题讨论】:

@Rajesh 替换为? 看起来不一样吗? 【参考方案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()

【讨论】:

请仅目标 c 两者都实现了代码,但都没有工作,甚至无法检查它们是否被调用。 它们被调用,检查只需转到您的应用程序目标 -> 编辑方案 -> 等待可执行文件启动。在这些方法中添加断点并点击你的通知 何时启动应用程序。应用程序关闭,然后推送接收,然后点击通知面板。那么,何时启动应用程序。 首先,请告诉我是否可以在应用关闭时从通知点击重定向到任何屏幕?【参考方案2】:

我最近做了一些事情,但它在 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 的实例,因此您可能需要确保您有一个导航控制器的实例来推送您的视图。

希望有帮助

【讨论】:

【参考方案3】:

当应用终止后,您可以从launchOptions 找到推送通知负载

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

【讨论】:

【参考方案4】:

您应该在 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];

【讨论】:

以上是关于当应用程序处于终止模式时,如何从推送通知重定向到特定的视图控制器的主要内容,如果未能解决你的问题,请参考以下文章

当应用程序处于后台或终止状态时如何增加推送通知批处理计数

重定向以查看 IOS 通知栏中的点击推送消息

当应用程序处于终止状态时,iOS 静默推送通知不会收到

当应用程序处于后台状态时,FCM多次推送通知无法正常工作

当应用程序处于终止状态而没有用户交互时如何清除本地通知。

当应用程序处于后台/终止并收到 FCM 通知时,如何在 Flutter 中显示本地通知?