[英]App crash when launch it with a notification
在我的远程通知实现中,我这样做是
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (launchOptions) {
NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
NSDictionary *dictionary=[userInfo objectForKey:@"jsonContent"];
dm.notificationDictionary=nil;
dm.notificationDictionary=dictionary;
NSDictionary *aps=[userInfo objectForKey:@"aps"];
dm.badgeNumber=[[aps valueForKey:@"badge"] intValue];
}
viewController1 = [[SplashViewController alloc] initWithNibName:@"SplashViewController" bundle:nil];
UINavigationController *aNavigationController=[[UINavigationController alloc] initWithRootViewController:viewController1];
self.navigationcontroller = aNavigationController ;
self.navigationcontroller.navigationBar.hidden=YES;
[self.window setRootViewController:self.navigationcontroller];
[self.window makeKeyAndVisible];
return YES;
}
然后
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
dm=[DataManager sharedManager];
ws=[[WebService alloc] init];
NSLog(@"%@",userInfo);
dm.isFroTabNotify=NO;
NSDictionary *dictionary=[userInfo objectForKey:@"jsonContent"];
dm.notificationDictionary=nil;
dm.notificationDictionary=dictionary;
NSDictionary *aps=[userInfo objectForKey:@"aps"];
dm.badgeNumber=[[aps valueForKey:@"badge"] intValue];
arrayHeader=[ws GetNotificationHeaders];//--------To get unread notification count
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyPersonalNotification" object:[aps valueForKey:@"badge"]];
if (application.applicationState==UIApplicationStateActive) {
[AGPushNoteView showWithNotificationMessage:[dm.notificationDictionary valueForKey:@"message"]];
if([[dm.notificationDictionary valueForKey:@"type"] intValue]==12)
{
dm=[DataManager sharedManager];
dm.ifFromNotifi=YES;
dm.intNotifyType=[[dm.notificationDictionary valueForKey:@"type"] intValue];
DataClass *obj=[DataClass getInstance];
dm.isUserLoggedin=NO;
dm.profImage=nil;
dm=[[DataManager alloc]init];
FrontViewController *frontViewController = [[FrontViewController alloc] init];
RearViewController *rearViewController = [[RearViewController alloc] init];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
[self.window setRootViewController:mainRevealController];
[self.window makeKeyAndVisible];
}
[AGPushNoteView setMessageAction:^(NSString *message) {
// Do something...
[ws setMarkAsReadUnread:[NSString stringWithFormat:@"%@",[dm.notificationDictionary valueForKey:@"type"]] :[NSString stringWithFormat:@"%@",[dm.notificationDictionary valueForKey:@"id"]]];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:dm.badgeNumber-1];
if ([[dm.notificationDictionary valueForKey:@"type"] intValue]==1||[[dm.notificationDictionary valueForKey:@"type"] intValue]==5) {
dm=[DataManager sharedManager];
dm.ifFromNotifi=YES;
dm.intNotifyType=[[dm.notificationDictionary valueForKey:@"type"] intValue];
dm.selectedBookingID=[[dm.notificationDictionary valueForKey:@"id"] intValue];
OneBookingViewController *frontViewController = [[OneBookingViewController alloc] init];
RearViewController *rearViewController = [[RearViewController alloc] init];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
[rearViewController.lblNotificationCount setText:[NSString stringWithFormat:@"%i",dm.badgeNumber]];
[self.window setRootViewController:mainRevealController];
[self.window makeKeyAndVisible];
}
else if ([[dm.notificationDictionary valueForKey:@"type"] intValue]==2)
{
dm=[DataManager sharedManager];
ws=[[WebService alloc] init];
dm.ifFromNotifi=YES;
NSString *strBidID=[NSString stringWithFormat:@"%@",[dm.notificationDictionary valueForKey:@"id"]];
[ws GetOneBidReceived:strBidID];
BidReceiveShowViewController *frontViewController = [[BidReceiveShowViewController alloc] init];
RearViewController *rearViewController = [[RearViewController alloc] init];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
[rearViewController.lblNotificationCount setText:[NSString stringWithFormat:@"%i",dm.badgeNumber]];
[self.window setRootViewController:mainRevealController];
[self.window makeKeyAndVisible];
}
else if ([[dm.notificationDictionary valueForKey:@"type"] intValue]==3 || [[dm.notificationDictionary valueForKey:@"type"] intValue]==4)
{
dm=[DataManager sharedManager];
dm.ifFromNotifi=YES;
NSString *strBidID=[NSString stringWithFormat:@"%@",[dm.notificationDictionary valueForKey:@"id"]];
[ws GetOneMyBid:strBidID];
MyBidsViewViewController *frontViewController = [[MyBidsViewViewController alloc] init];
RearViewController *rearViewController = [[RearViewController alloc] init];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
[rearViewController.lblNotificationCount setText:[NSString stringWithFormat:@"%i",dm.badgeNumber]];
[self.window setRootViewController:mainRevealController];
[self.window makeKeyAndVisible];
}
else if ([[dm.notificationDictionary valueForKey:@"type"] intValue]==6)
{
dm=[DataManager sharedManager];
dm.ifFromNotifi=YES;
dm.intNotifyType=[[dm.notificationDictionary valueForKey:@"type"] intValue];
dm.selectedBookingID=[[dm.notificationDictionary valueForKey:@"id"] intValue];
OneTripViewController *frontViewController = [[OneTripViewController alloc] init];
RearViewController *rearViewController = [[RearViewController alloc] init];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
[rearViewController.lblNotificationCount setText:[NSString stringWithFormat:@"%i",dm.badgeNumber]];
[self.window setRootViewController:mainRevealController];
[self.window makeKeyAndVisible];
}
else if ([[dm.notificationDictionary valueForKey:@"type"] intValue]==7)
{
dm=[DataManager sharedManager];
dm.ifFromNotifi=YES;
dm.selectedReviewID=[[dm.notificationDictionary valueForKey:@"id"] intValue];
MyReviewsViewController *frontViewController = [[MyReviewsViewController alloc] init];
RearViewController *rearViewController = [[RearViewController alloc] init];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
[rearViewController.lblNotificationCount setText:[NSString stringWithFormat:@"%i",dm.badgeNumber]];
[self.window setRootViewController:mainRevealController];
[self.window makeKeyAndVisible];
}
else if ([[dm.notificationDictionary valueForKey:@"type"] intValue]==8)
{
dm=[DataManager sharedManager];
dm.ifFromNotifi=YES;
dm.selectedBookingID=[[dm.notificationDictionary valueForKey:@"id"] intValue];
MyTravelsViewController *frontViewController = [[MyTravelsViewController alloc] init];
RearViewController *rearViewController = [[RearViewController alloc] init];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
[rearViewController.lblNotificationCount setText:[NSString stringWithFormat:@"%i",dm.badgeNumber]];
[self.window setRootViewController:mainRevealController];
[self.window makeKeyAndVisible];
}
}];
}
else
{
[ws setMarkAsReadUnread:[NSString stringWithFormat:@"%@",[dm.notificationDictionary valueForKey:@"type"]] :[NSString stringWithFormat:@"%@",[dm.notificationDictionary valueForKey:@"id"]]];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:dm.badgeNumber-1];
if ([dm.notificationDictionary count]>0) {
if ([[dm.notificationDictionary valueForKey:@"type"] intValue]==1||[[dm.notificationDictionary valueForKey:@"type"] intValue]==5) {
dm=[DataManager sharedManager];
dm.ifFromNotifi=YES;
dm.intNotifyType=[[dm.notificationDictionary valueForKey:@"type"] intValue];
dm.selectedBookingID=[[dm.notificationDictionary valueForKey:@"id"] intValue];
// dm.notificationDictionary=nil;
OneBookingViewController *frontViewController = [[OneBookingViewController alloc] init];
RearViewController *rearViewController = [[RearViewController alloc] init];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
dm.notificationDictionary=nil;
[rearViewController.lblNotificationCount setText:[NSString stringWithFormat:@"%i",dm.badgeNumber]];
[self.window setRootViewController:mainRevealController];
[self.window makeKeyAndVisible];
}
else if ([[dm.notificationDictionary valueForKey:@"type"] intValue]==2)
{
dm=[DataManager sharedManager];
ws=[[WebService alloc] init];
dm.ifFromNotifi=YES;
NSString *strBidID=[NSString stringWithFormat:@"%@",[dm.notificationDictionary valueForKey:@"id"]];
[ws GetOneBidReceived:strBidID];
BidReceiveShowViewController *frontViewController = [[BidReceiveShowViewController alloc] init];
RearViewController *rearViewController = [[RearViewController alloc] init];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
// dm.notificationDictionary=nil;
[self.window setRootViewController:mainRevealController];
[self.window makeKeyAndVisible];
[rearViewController.lblNotificationCount setText:[NSString stringWithFormat:@"%i",dm.badgeNumber]];
}
else if([[dm.notificationDictionary valueForKey:@"type"] intValue]==10)
{
dm=[DataManager sharedManager];
dm.ifFromNotifi=YES;
dm.SelectedRideID=[[dm.notificationDictionary valueForKey:@"id"] intValue];
dm.intNotifyType=[[dm.notificationDictionary valueForKey:@"type"] intValue];
RequestMatchToSellerViewController *frontViewController = [[RequestMatchToSellerViewController alloc] init];
RearViewController *rearViewController = [[RearViewController alloc] init];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
// dm.notificationDictionary=nil;
[rearViewController.lblNotificationCount setText:[NSString stringWithFormat:@"%i",dm.badgeNumber]];
[self.window setRootViewController:mainRevealController];
[self.window makeKeyAndVisible];
}
else if([[dm.notificationDictionary valueForKey:@"type"] intValue]==11)
{
dm=[DataManager sharedManager];
dm.ifFromNotifi=YES;
dm.selectedRequestid=[[dm.notificationDictionary valueForKey:@"id"] intValue];
dm.intNotifyType=[[dm.notificationDictionary valueForKey:@"type"] intValue];
RequestMatchToBuyerViewController *frontViewController = [[RequestMatchToBuyerViewController alloc] init];
RearViewController *rearViewController = [[RearViewController alloc] init];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
// dm.notificationDictionary=nil;
[rearViewController.lblNotificationCount setText:[NSString stringWithFormat:@"%i",dm.badgeNumber]];
[self.window setRootViewController:mainRevealController];
[self.window makeKeyAndVisible];
}
else if([[dm.notificationDictionary valueForKey:@"type"] intValue]==12)
{
dm=[DataManager sharedManager];
dm.ifFromNotifi=YES;
dm.intNotifyType=[[dm.notificationDictionary valueForKey:@"type"] intValue];
DataClass *obj=[DataClass getInstance];
dm.isUserLoggedin=NO;
dm.profImage=nil;
dm=[[DataManager alloc]init];
ws=[[WebService alloc] init];
[ws gcmDeviceRejection];
loggedWayKeychain=[[KeychainItemWrapper alloc] initWithIdentifier:@"LoggedWay" accessGroup:nil];
loginKeychain=[[KeychainItemWrapper alloc] initWithIdentifier:@"UserLogin" accessGroup:nil];
[loginKeychain resetKeychainItem];
[loggedWayKeychain resetKeychainItem];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
FrontViewController *frontViewController = [[FrontViewController alloc] init];
RearViewController *rearViewController = [[RearViewController alloc] init];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
[self.window setRootViewController:mainRevealController];
[self.window makeKeyAndVisible];
}
else if([[dm.notificationDictionary valueForKey:@"type"] intValue]==13)
{
dm=[DataManager sharedManager];
dm.ifFromNotifi=YES;
dm.intNotifyType=[[dm.notificationDictionary valueForKey:@"type"] intValue];
dm.strAdminMsgTitle=[dm.notificationDictionary valueForKey:@"subject"];
dm.strAdminMsg=[dm.notificationDictionary valueForKey:@"message"];
AdminMessagesViewController *frontViewController = [[AdminMessagesViewController alloc] init];
RearViewController *rearViewController = [[RearViewController alloc] init];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]
initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
[self.window setRootViewController:mainRevealController];
[self.window makeKeyAndVisible];
}
}
}
}
当应用程序在后台和前台运行时,效果很好。 但是,当应用程序未运行时,以及当我单击通知以启动应用程序时,应用程序崩溃。 这是什么原因,我该如何解决? 请帮我。 谢谢
正如我在方法中看到的那样- application:didReceiveRemoteNotification:fetchCompletionHandler:
您已为活动的两个状态UIApplicationStates
和所有其他状态(在else块中)编码,当应用程序被用户和远程通知杀死(强制退出)时,这可能是崩溃到达- application:didReceiveRemoteNotification:fetchCompletionHandler:
用户点击通知警报时,将调用- application:didReceiveRemoteNotification:fetchCompletionHandler:
方法。
根据苹果的文档,
注意:如果用户从系统显示的警报中打开您的应用程序,则系统可能会在您的应用程序即将进入前台时再次调用此方法,以便您可以更新用户界面并显示与通知有关的信息。
因此,认为您应该研究代码,必须对导致错误的任何代码块进行两次调用,尤其是在初始化UIViewController
对象或UINavigationController
对象的地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.