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

查看:25
本文介绍了收到 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 推送通知有关.

My question is related with the iOS push notifications.

推荐答案

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

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

-didReceiveRemoteNotification上打个断点,看是否在不同的场景下执行,看是否在if-条件之内.

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

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

-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/documentation/uikit/uiapplicationdelegate

如果应用程序正在运行并收到远程通知,则应用程序调用此方法来处理通知.你的实施这种方法应该使用通知采取适当的课程行动.
...
如果在推送通知到达时应用程序未运行,则该方法启动应用程序并在启动选项字典.app没有调用这个方法来处理那个推送通知.相反,你的实现application:willFinishLaunchingWithOptions:application: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
    }
    //...
}

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

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

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