本文介绍了收到iOS推送通知时打开视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to open a specific view controller when a user clicks on the received push notification message, but when I receive a push notification message and click the message, only the application opens, but it does not redirect to a specific view controller.

我的代码是

-(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推送通知有关。 / p>

My question is related with the iOS push notifications.

推荐答案

您可能遇到了 if(applicationIsActive)条件的问题。

You may be having issues with the if (applicationIsActive) condition.

-didReceiveRemoteNotification 上放置一个断点,看看它是否在不同的场景下执行,看看它是否在如果 - 条件

Put a breakpoint on -didReceiveRemoteNotification and see whether it executes in different scenarios and see if it goes within the if-condition.

(与某一程度无关,但值得检查):

(unrelated to a certain extent but worth checking) this question:
didReceiveRemoteNotification when in background

-didReceiveRemoteNotification )已关闭,您点击推送通知以打开应用程序。

此方法在应用程序处于前台或应用程序从后台转换到前台时收到推送通知时执行。

-didReceiveRemoteNotification will not execute if your app was (initially) closed and you clicked on the push notification to open the app.
This method executes when a push notification is received while the application is in the foreground or when the app transitions from background to foreground.

Apple参考:






所以...当应用程式不正在运行,并且系统接收到推送通知,当用户点击推送通知时,即会启动该应用并现在 ...推送通知内容将显示在 -didFinishLaunchingWithOptions:方法中的 launchOptions 参数。


So... When the app is not running and a push notification is received, when the user clicks on the push notification, the app is launched and now... the push notification contents will be available in the -didFinishLaunchingWithOptions: method in it's launchOptions parameter.

换句话说... -didReceiveRemoteNotification 将不会执行此时间,您还需要这个:

In other words... -didReceiveRemoteNotification won't execute this time and you'll also need to do this:

- (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
    }
    //...
}

=https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW4> Apple's Doc on Handling Local和远程通知

Also read Apple's Doc on Handling Local and Remote Notifications

这篇关于收到iOS推送通知时打开视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 08:54