问题描述
我想在用户点击收到的推送通知消息时打开一个特定的视图控制器,但是当我收到一个推送通知消息并单击该消息时,只有应用程序打开,但它没有重定向到特定的视图控制器.
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: 方法需要获取推送通知有效负载数据并做出适当响应.
所以...当应用未运行并收到推送通知时,当用户点击推送通知时,应用将启动并且现在... 推送通知内容将在 -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
}
//...
}
这篇关于收到 iOS 推送通知时打开视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!