当我的应用程序处于后台时,仅当我触摸顶部通知横幅并且在我单击应用程序图标时不工作时才会处理推送通知

ars*_*had 5 iphone objective-c apple-push-notifications ios

我已经实现application:didReceiveRemoteNotification:了在收到推送通知时在我的应用中存储数据.
但是,当我的应用程序处于后台并且收到通知时,仅当我触摸顶部显示的通知横幅时才会存储数据:

相反,如果我触摸应用程序图标以重新打开它,则不会存储通知的内容:

application:didReceiveRemoteNotification: 仅当我将通知横幅推到顶部时才会调用.

我已经使用applicationWillEnterForegrounddidFinishLaunchingWithOptions方法,同时单击应用程序图标并调试其输入applicationWillEnterForeground和控制无处可去.这是didFinishLaunchingWithOptionsapplicationWillEnterForeground和的代码didReceiveRemoteNotification.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    self.isForeground = YES;

    // Let the device know we want to receive push notifications
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    storage= [[NSMutableArray alloc]init];
    if (launchOptions != nil) {
        // launched from notification item click
        NSDictionary *userInfo = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
        if (userInfo != nil) [self HandleNotification:userInfo];
    }
    return YES;
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    isForeground = YES;
    NSArray *subviews = [window subviews];
    for (int i = 0; i < [subviews count]; i++) {
        [[subviews objectAtIndex:i] removeFromSuperview];
    }
    //[self.window addSubview:tabBarController.view];
    [self.window makeKeyAndVisible];
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController; 
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    [self HandleNotification:userInfo]; 
}

- (void)HandleNotification:(NSDictionary *)userInfo {
    ApiWrapper *wrapper = [[ApiWrapper alloc] init];
    NSString *dteStr = [[NSString alloc] init];
    NSDate *nowdate = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    //[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/London"]];
    [dateFormat setDateFormat:@"dd/MM/yyyy HH:mm:ss"];
    dteStr = [dateFormat stringFromDate:nowdate];
    [dateFormat release];

    NSString *notifId = [userInfo objectForKey:@"NotificationId"];
    NSData *test = self.strTest;
    NSString *strToken = [NSString stringWithFormat:@"%@", test];
    strToken = [strToken substringWithRange:NSMakeRange(1, [strToken length] - 2)];     

    [wrapper deviceResponse:notifId:dteStr:strToken];

    NSLog(@".....user info%@", userInfo);
    NSDictionary *pushInfo = [userInfo  objectForKey:@"aps"];
    NSString *alertstring = [pushInfo objectForKey:@"alert"];
    NSLog(@"Alertstring: %@", alertstring);

    [UIApplication sharedApplication].applicationIconBadgeNumber = [[[userInfo objectForKey:@"aps"] objectForKey: @"badgecount"] intValue];

    MLNotifMessage *objNotif = [[MLNotifMessage alloc] init];
    objNotif.notifText = alertstring;    
    NSDate *nowdate1 = [NSDate date];
    NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
    //[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/London"]];
    [dateFormat1 setDateFormat:@"dd/MM/yyyy HH:mm:ss"];
    objNotif.datenow = [dateFormat1 stringFromDate:nowdate1];
    [dateFormat1 release];

    NSLog(@"Date in delegate class is %@", objNotif.datenow);
    [storage addObject:objNotif];    

    if (self.isForeground) {
        NSArray *subviews = [window subviews];
        for (int i = 0; i < [subviews count]; i++) {
            [[subviews objectAtIndex:i] removeFromSuperview];
        }
        [self.window makeKeyAndVisible];
        self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
        self.window.rootViewController = self.viewController;
    }
}
Run Code Online (Sandbox Code Playgroud)

iLe*_*ner 1

如果单击主屏幕上的应用程序图标,您将无法获取推送通知的数据。但还有办法,每当应用程序进入前台时,您可以向服务器发送一个小的有效负载,然后要求服务器立即发送推送通知。

另请看一下:您的问题可能与它重复。

希望这对您有帮助。