收到iOS推送通知时打开视图控制器 [英] Open a view controller when a iOS push notification is received

查看:295
本文介绍了收到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.

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

在后台执行didReceiveRemoteNotification

(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参考: https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/doc / uid / TP40006786-CH3-SW16


如果应用程式正在执行并收到远端通知,应用程式
调用此方法来处理通知。您的实现
此方法应该使用通知采取适当的课程
的行动。

...

如果应用程序没有运行时推送通知到达,方法
启动应用程序,并在
启动选项字典中提供相应的信息。应用程序不调用此方法来处理推送通知的
。相反,您实施的
应用程序:willFinishLaunchingWithOptions:
应用程序:didFinishLaunchingWithOptions:方法需要获取
推送通知有效负载数据

If the app is running and receives a remote notification, the app calls this method to process the notification. Your implementation of this method should use the notification to take an appropriate course of action.
...
If the app is not running when a push notification arrives, the method launches the app and provides the appropriate information in the launch options dictionary. The app does not call this method to handle that push notification. Instead, your implementation of the application:willFinishLaunchingWithOptions: or application:didFinishLaunchingWithOptions: method needs to get the push notification payload data and respond appropriately.






所以...当应用程式不正在运行,并且系统接收到推送通知,当用户点击推送通知时,即会启动该应用并现在 ...推送通知内容将显示在 -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推送通知时打开视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆