[英]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.