收到iOS推送通知时打开视图控制器
我想在用户点击收到的推送通知消息时打开一个特定的视图控制器,但是当我收到推送通知消息并单击该消息时,只有应用程序打开,但不会redirect到特定的视图控制器。
我的代码是
-(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推送通知。
您可能会遇到if (applicationIsActive)
条件的问题。
在-didReceiveRemoteNotification
上放置一个断点,看它是否在不同的场景下执行,看看它是否在if条件之内。
( 在一定程度上无关,但值得检查 )这个问题:
在后台执行didReceiveRemoteNotification
注意:
-didReceiveRemoteNotification
将不会执行,如果你的应用程序是( 最初 )closures,你点击推送通知打开应用程序。
当应用程序处于前台或者应用程序从后台转换到前台时收到推送通知时执行此方法。
Apple参考手册: https : //developer.apple.com/documentation/uikit/uiapplicationdelegate
如果应用程序正在运行并收到远程通知,则应用程序会调用此方法来处理通知。 此方法的实施应使用通知来采取适当的行动。
…
如果在推送通知到达时应用程序没有运行,该方法将启动应用程序并在启动选项字典中提供适当的信息。 该应用程序不会调用此方法来处理推送通知。 相反,您的应用程序的实现:willFinishLaunchingWithOptions:或应用程序: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 } //... }
另请阅读Apple处理本地和远程通知的文档
标识符名称中有一个额外的空间。 删除它,并尝试:
UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; PushBildirimlerim* pvc = [mainstoryboard instantiateViewControllerWithIdentifier:@"PushBildirimlerim"]; [self.window.rootViewController presentViewController:pvc animated:YES completion:NULL];
我有同样的问题,当应用程序被暂停/终止和推送通知到达我的应用程序只是打开,而不是redirect到特定的屏幕对应的通知解决scheme是,
在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
此方法的参数launchOptions
告诉我们是否有通知,检查是否需要调用方法redirect到特定的屏幕
代码如下…
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //your common or any code will be here at last add the below code.. NSMutableDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if (notification) { //this notification dictionary is same as your JSON payload whatever you gets from Push notification you can consider it as a userInfo dic in last parameter of method -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo NSLog(@"%@",notification); [self showOfferNotification:notification]; } return YES; }
然后在方法showOfferNotification:通知你可以redirect用户到相应的屏幕,如…
//** added code for notification -(void)showOfferNotification:(NSMutableDictionary *)offerNotificationDic{ //This whole is my coding stuff.. your code will come here.. NSDictionary *segueDictionary = [offerNotificationDic valueForKey:@"aps"]; NSString *segueMsg=[[NSString alloc]initWithFormat:@"%@",[segueDictionary valueForKey:@"alert"]]; NSString *segueID=[[NSString alloc]initWithFormat:@"%@",[offerNotificationDic valueForKey:@"id"]]; NSString *segueDate=[[NSString alloc]initWithFormat:@"%@",[offerNotificationDic valueForKey:@"date"]]; NSString *segueTime=[[NSString alloc]initWithFormat:@"%@",[offerNotificationDic valueForKey:@"time"]]; NSLog(@"Show Offer Notification method : segueMsg %@ segueDate %@ segueTime %@ segueID %@",segueMsg,segueDate,segueTime,segueID); if ([segueID isEqualToString:@"13"]){ NSString *advertisingUrl=[[NSString alloc]initWithFormat:@"%@",[offerNotificationDic valueForKey:@"advertisingUrl"]]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:segueMsg forKey:@"notificationMsg"]; [defaults setObject:segueDate forKey:@"notifcationdate"]; [defaults setObject:segueTime forKey:@"notifcationtime"]; [defaults setObject:advertisingUrl forKey:@"advertisingUrl"]; [defaults synchronize]; navigationController = (UINavigationController *)self.window.rootViewController; UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle: nil]; FLHGAddNotificationViewController *controller = (FLHGAddNotificationViewController*)[mainStoryboard instantiateViewControllerWithIdentifier: @"offerViewController"]; [navigationController pushViewController:controller animated:YES]; } }