gpt4 book ai didi

ios - 如果应用程序关闭并且我收到推送通知,如何检测通知点击并转到特定 Controller ?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:56:32 24 4
gpt4 key购买 nike

我有一个 iOS 应用程序,我在其中使用推送通知在发布正确答案时通知用户。如果应用程序打开并且我单击通知,它会转到指定的 Controller 。但是如果应用程序关闭并且我收到通知,它不会转到指定的 Controller 。

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"User Info %@",userInfo);
NSString* alertValue = [[userInfo valueForKey:@"aps"] valueForKey:@"badge"];
NSLog(@"my message-- %@",alertValue);
int badgeValue= [alertValue intValue];

// NSNumber *identifierString = [[[userInfo valueForKey:@"aps"]valueForKey:@"details"]valueForKey:@"identifire"];
// NSLog(@"identifierString %@",identifierString);
NSString *alertMessage = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
NSLog(@"ALertMessage %@",alertMessage);
if ([alertMessage isEqualToString:@"New answer added."]) {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:badgeValue];
// [[NSUserDefaults standardUserDefaults] setObject:[[[[userInfo valueForKey:@"aps"] valueForKey:@"details"] objectAtIndex:0]valueForKey:@"question"]forKey:@"notificationQuestion"];
// [[NSUserDefaults standardUserDefaults] setObject:[[[[userInfo valueForKey:@"aps"] valueForKey:@"details"] objectAtIndex:0]valueForKey:@"user_image"]forKey:@"notificationImage"];
// NSLog(@"Image %@",[[NSUserDefaults standardUserDefaults]valueForKey:@"notificationQuestion"]);


NSLog(@"User Information %@",userInfo);
NSLog(@"User Details %@",[[userInfo valueForKey:@"aps"] valueForKey:@"details"]);
// NSLog(@"User Details 1%@",[[[userInfo valueForKey:@"aps"] valueForKey:@"details"] objectAtIndex:0]);
// NSLog(@"User Details 1%@",[[[[userInfo valueForKey:@"aps"] valueForKey:@"details"] objectAtIndex:0]valueForKey:@"question"]);
pushDictonary = [[userInfo valueForKey:@"aps"] valueForKey:@"details"];


}


//NSArray *pushDetails = [[userInfo valueForKey:@"aps"] valueForKey:@"details"];
// NSLog(@"Push Details %@",pushDetails);
if (application.applicationState == UIApplicationStateActive ) {

// UILocalNotification *localNotification = [[UILocalNotification alloc] init];
// localNotification.userInfo = userInfo;
// localNotification.soundName = UILocalNotificationDefaultSoundName;
// localNotification.alertBody = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
// localNotification.fireDate = [NSDate date];
if ([alertMessage isEqualToString:@"New answer added."])
{
[self createNotificationViewwithUserDictionary:userInfo];

}


}
else if (application.applicationState == UIApplicationStateBackground)
{
NSLog(@"YES");
}
else if (application.applicationState == UIApplicationStateInactive)
{
NSLog(@"YES");
if ([alertMessage isEqualToString:@"New answer added."])
[self pushdetailsViewController];
}

去 Controller :

   -(void)pushdetailsViewController
{

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
QPYourQuestionController *controller = (QPYourQuestionController*)[mainStoryboard instantiateViewControllerWithIdentifier: @"yourQuestion"];
[[QPCommonClass initializeUserDefaults]setObject:[pushDictonary valueForKey:@"question_id"] forKey:@"currentquestionID"];
NSLog(@"Dictionary %@",pushDictonary);

UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
[navigationController pushViewController:controller animated:YES];



}

最佳答案

针对应用关闭时的远程通知委托(delegate)

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

没有被调用

在这种情况下,调用转到以下方法:

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

因此您可以这样做,将用户带到您想要的 VC

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ...

if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
// Do whatever you want
}
}

关于ios - 如果应用程序关闭并且我收到推送通知,如何检测通知点击并转到特定 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38473585/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com