收到 iOS 推送通知时打开视图控制器

2024-02-27

我想在用户单击收到的推送通知消息时打开特定的视图控制器,但是当我收到推送通知消息并单击该消息时,仅打开应用程序,但不会重定向到特定的视图控制器。

我的代码是

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    if (applicationIsActive) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Bildirim"
                                                            message:[NSString stringWithFormat:@"%@ ",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]]
                                                           delegate:self cancelButtonTitle:@"Ok" 
                                                  otherButtonTitles:nil];
        [alertView show];

        UIViewController *vc = self.window.rootViewController;
        PushBildirimlerim *pvc = [vc.storyboard instantiateViewControllerWithIdentifier:@"PushBildirimlerim "];

        [vc presentViewController:pvc animated:YES completion:nil];
     }
}

我的问题与 iOS 推送通知有关。


您可能遇到以下问题if (applicationIsActive)健康)状况。

打个断点-didReceiveRemoteNotification看看它是否在不同的场景中执行,看看它是否在if-健康)状况。

(在一定程度上无关但值得检查) 这个问题:
在后台时的 didReceiveRemoteNotification https://stackoverflow.com/q/5056689/2857130


Note:

-didReceiveRemoteNotification如果您的应用程序是(最初)关闭,您单击推送通知以打开应用程序。
当应用程序位于前台时或应用程序从后台转换到前台时收到推送通知时,将执行此方法。

苹果参考:https://developer.apple.com/documentation/uikit/uiapplicationdelegate https://developer.apple.com/documentation/uikit/uiapplicationdelegate

如果应用程序正在运行并收到远程通知,则应用程序 调用该方法来处理通知。您的实施 此方法应该使用通知来参加适当的课程 的行动。
...
如果推送通知到达时应用程序未运行,则该方法 启动应用程序并在中提供适当的信息 启动选项字典。应用程序不会调用此方法来处理 那个推送通知。相反,您的实施应用程序:willFinishLaunchingWithOptions: or 应用程序:didFinishLaunchingWithOptions:方法需要得到 推送通知有效负载数据并做出适当响应。


所以...当应用程序不运行并收到推送通知,当用户单击推送通知时,应用程序将启动并now...推送通知内容将在-didFinishLaunchingWithOptions:其中的方法launchOptions范围。

换句话说...-didReceiveRemoteNotification这次不会执行,您还需要执行以下操作:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //...
    NSDictionary *userInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
    NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];

    if(apsInfo) {
        //there is some pending push notification, so do something
        //in your case, show the desired viewController in this if block
    }
    //...
}

另请阅读Apple 关于处理本地和远程通知的文档 https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/index.html#//apple_ref/doc/uid/TP40008194-CH103-SW4

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

收到 iOS 推送通知时打开视图控制器 的相关文章

随机推荐