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

cds*_*oft 28 cocoa-touch push-notification ios appdelegate

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

我的代码是

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

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

sta*_*Man 45

您可能遇到if (applicationIsActive)病情问题.

设置断点-didReceiveRemoteNotification并查看它是否在不同的场景中执行,并查看它是否if -condition 范围内.

(在某种程度上无关但值得检查)这个问题:
didReceiveRemoteNotification在后台


注意:

-didReceiveRemoteNotification如果您的应用(最初)已关闭并且您点击推送通知以打开应用,则不会执行.
当应用程序在前台或应用程序从后台转换为前台时收到推送通知时,将执行此方法.

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

如果应用程序正在运行并收到远程通知,则应用程序会调用此方法来处理通知.您对此方法的实现应使用通知采取适当的操作过程.
...
如果在推送通知到达时应用程序未运行,该方法将启动应用程序并在启动选项字典中提供相应的信息.该应用程序不会调用此方法来处理该推送通知.相反,您的应用程序的实现 :willFinishLaunchingWithOptions:application:didFinishLaunchingWithOptions:方法需要获取推送通知有效负载数据并做出适当的响应.


所以...当应用程序没有运行并且收到推送通知时,当用户点击推送通知时,应用程序就会启动,现在 ......推送通知内容将-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
    }
    //...
}
Run Code Online (Sandbox Code Playgroud)

另请阅读Apple关于处理本地和远程通知的文档

  • 如果您的应用程序处于后台并因为用户触摸推送通知而进入前台,则app不会通过`appliaction:didFinishLaunchingWithOptions`(因为它已经启动)并触发`application:didReceiveRemoteNotification:` (8认同)

Tos*_*lji 6

标识符名称中有一个额外的空格.删除它并尝试:

UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
PushBildirimlerim* pvc = [mainstoryboard instantiateViewControllerWithIdentifier:@"PushBildirimlerim"];
[self.window.rootViewController presentViewController:pvc animated:YES completion:NULL];
Run Code Online (Sandbox Code Playgroud)