2014-10-07 16 views
0

我收到来自服务器的推送通知,但是我的postNotification没有被调用。它正确地输入了if语句,该语句声明类型== 1.所有用户默认值都被正确存储,但通知未被调用。我在视图控制器的函数中放置了断点以及警报,并通过后置通知调用该函数。但它永远不会被调用。这里是我的代码:未从应用程序调用发布通知didFinishLaunchingWithOptions

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
if(launchOptions != nil) 
    { 
    NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; 
    if (dictionary != nil) 
    { 
     NSString *type = [dictionary objectForKey:@"type"]; 
     if([[NSString stringWithFormat:@"%@",type]integerValue]==1) 
     { 

      [[NSUserDefaults standardUserDefaults]setObject:[[dictionary objectForKey:@"aps"] valueForKey:@"alert"] forKey:@"message"]; 
      [[NSUserDefaults standardUserDefaults]setObject:[dictionary objectForKey:@"senderid"] forKey:@"senderID"]; 
      [[NSUserDefaults standardUserDefaults]setObject:[dictionary objectForKey:@"sendername"] forKey:@"sendername"]; 
      [[NSUserDefaults standardUserDefaults]setObject:[dictionary objectForKey:@"userimage"] forKey:@"pushImage"]; 
      [[NSUserDefaults standardUserDefaults]synchronize]; 

      NSLog(@"message is %@",[[dictionary objectForKey:@"aps"] valueForKey:@"alert"]); 
      NSLog(@"senderID is %@",[dictionary objectForKey:@"senderid"]); 
      NSLog(@"sender name is %@",[dictionary objectForKey:@"sendername"]); 

      [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:launchOptions]; 

     } 

    } 

#ifdef __IPHONE_8_0 
if(NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) 
{ 
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert) categories:nil]; 
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 
} 
#endif 

UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound |  UIRemoteNotificationTypeNewsstandContentAvailability; 
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes]; 


application.applicationIconBadgeNumber=0; 
[UIApplication sharedApplication].statusBarHidden = NO; 
[UIApplication sharedApplication].networkActivityIndicatorVisible=FALSE; 
return YES; 
} 

回答

0

找到我自己的问题的答案。这里是代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
[[self window] setBackgroundColor:[UIColor whiteColor]]; 
[FBLoginView class]; 
self.mainNavigationController = (UINavigationController*)self.window.rootViewController; 


if(launchOptions != nil) 
{ 
    NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; 

    if (dictionary != nil) 
    { 
     NSString *type = [NSString stringWithFormat:@"%@",[[[dictionary valueForKey:@"aps"] valueForKey:@"json"] valueForKey:@"type"]]; 
     NSLog(@"type is %@",type); 

     if ([type isEqualToString:@"0"]) // 0 means NOW 
     { 
      [[NSUserDefaults standardUserDefaults]setObject:[[[dictionary valueForKey:@"aps"] valueForKey:@"json"] valueForKey:@"yadda_sub_type"] forKey:kYaddaSubType_Push]; 
      [[NSUserDefaults standardUserDefaults]setObject:[[[dictionary valueForKey:@"aps"] valueForKey:@"json"] valueForKey:@"user_id"] forKey:kYaddaUserID_Push]; 
      [[NSUserDefaults standardUserDefaults]setObject:[[[dictionary valueForKey:@"aps"] valueForKey:@"json"] valueForKey:@"yadda_id"] forKey:kYaddaID_Push]; 

      ReciversSideVC *reciverControoler = [[ReciversSideVC alloc]init]; 
      [reciverControoler.navigationController setNavigationBarHidden:YES]; 
      [reciverControoler.navigationController setToolbarHidden:YES]; 
      [AppDelegateClass.mainNavigationController pushViewController:reciverControoler animated:NO]; 
     } 

     else if ([type isEqualToString:@"1"]) // 1 means LATER 
     { 
      if (isCalendarScreenOpen==YES) 
      { 
       CalendarVC *calendar = [[CalendarVC alloc]init]; 
       [calendar.navigationController setNavigationBarHidden:YES]; 
       [calendar.navigationController setToolbarHidden:YES]; 
       [AppDelegateClass.mainNavigationController pushViewController:calendar animated:NO]; 
      } 
     } 

     else if ([type isEqualToString:@"2"]) // 2 means Remind me 
     { 
      LandingPageVC *landing = [[LandingPageVC alloc]init]; 
      [landing.navigationController setNavigationBarHidden:YES]; 
      [landing.navigationController setToolbarHidden:YES]; 
      [AppDelegateClass.mainNavigationController pushViewController:landing animated:NO]; 
     } 

     else if ([type isEqualToString:@"3"]) // 3 means Reply 
     { 
      LandingPageVC *landing = [[LandingPageVC alloc]init]; 
      [landing.navigationController setNavigationBarHidden:YES]; 
      [landing.navigationController setToolbarHidden:YES]; 
      [AppDelegateClass.mainNavigationController pushViewController:landing animated:NO]; 
     } 

     else if ([type isEqualToString:@"4"]) // 4 means Contacts 
     { 
      [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"flag_CallAPI"]; 
      [[NSUserDefaults standardUserDefaults]synchronize]; 
      ContactDetailVC *contactDetails = [[ContactDetailVC alloc]init]; 
      contactDetails.userID = [[[dictionary valueForKey:@"aps"] valueForKey:@"json"] valueForKey:@"user_id"]; 
      contactDetails.phoneNumberFromServer = [[[dictionary valueForKey:@"aps"] valueForKey:@"json"] valueForKey:@"phone_number"]; 
      [contactDetails.navigationController setNavigationBarHidden:YES]; 
      [contactDetails.navigationController setToolbarHidden:YES]; 
      [AppDelegateClass.mainNavigationController pushViewController:contactDetails animated:NO]; 
     } 

    } 
} 


UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; 
if (localNotif != nil) 
{ 
    NSDictionary *NotificationValue = [localNotif.userInfo objectForKey:@"localPushNotification"]; 

    if (NotificationValue != nil) 
    { 
     NSLog(@"Run with notification value: %@", NotificationValue); 

     [[NSUserDefaults standardUserDefaults]setObject:[NotificationValue valueForKey:@"yadda_sub_type"] forKey:kYaddaSubType_Push]; 
     [[NSUserDefaults standardUserDefaults]setObject:[NotificationValue valueForKey:@"user_id"] forKey:kYaddaUserID_Push]; 
     [[NSUserDefaults standardUserDefaults]setObject:[NotificationValue valueForKey:@"yadda_id"] forKey:kYaddaID_Push]; 

     ReciversSideVC *reciverControoler = [[ReciversSideVC alloc]init]; 
     [AppDelegateClass.mainNavigationController pushViewController:reciverControoler animated:NO]; 

    } 
} 


if(NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) 
{ 
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert) categories:nil]; 
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 
} 

UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound |  UIRemoteNotificationTypeNewsstandContentAvailability; 
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes]; 

application.applicationIconBadgeNumber=0; 
[UIApplication sharedApplication].statusBarHidden = NO; 
[UIApplication sharedApplication].networkActivityIndicatorVisible=FALSE; 

return YES; 
} 
相关问题