不从应用程序didFinishLaunchingWithOptions调用Post Notification

时间:2014-10-07 07:56:30

标签: objective-c ios8 push xcode6 nsnotificationcenter

我从服务器收到推送通知但我的postNotification没有被调用。它正确输入if语句,表明类型== 1.所有用户默认值都已正确存储但未通知通知。我把断点和警报放在视频控制器的功能中,后者通过发布通知调用。但它永远不会被召唤。这是我的代码:

- (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;
 }

1 个答案:

答案 0 :(得分: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;
}