2013-06-04 36 views
5

Tôi đã triển khai application:didReceiveRemoteNotification: để lưu trữ dữ liệu trong ứng dụng của mình khi nhận được thông báo đẩy.
Tuy nhiên, khi ứng dụng của tôi ở chế độ nền và tôi nhận được thông báo, dữ liệu chỉ được lưu trữ nếu tôi chạm vào biểu ngữ thông báo xuất hiện ở trên cùng:Khi ứng dụng của tôi ở chế độ nền, thông báo đẩy chỉ được xử lý nếu tôi chạm vào biểu ngữ thông báo trên cùng và không hoạt động khi tôi nhấp vào biểu tượng ứng dụng

Thay vào đó, nếu tôi chạm vào biểu tượng ứng dụng để mở lại, nội dung của thông báo không được lưu trữ:

application:didReceiveRemoteNotification: chỉ được gọi khi tôi đẩy biểu ngữ thông báo lên trên cùng.

Tôi đã sử dụng các phương pháp applicationWillEnterForegrounddidFinishLaunchingWithOptions và trong khi nhấp vào biểu tượng ứng dụng và gỡ lỗi nhập applicationWillEnterForeground và điều khiển của bạn không thành công. Đây là mã cho didFinishLaunchingWithOptionsapplicationWillEnterForegrounddidReceiveRemoteNotification.

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

Khi bạn nhấn vào biểu tượng ứng dụng, ứng dụng sẽ mở ra. Ứng dụng chỉ được yêu cầu xử lý thông báo nếu người dùng nhấn cụ thể vào ứng dụng đó. – Greg

Trả lời

1

Bạn không thể nhận dữ liệu để thông báo đẩy nếu được nhấp vào biểu tượng ứng dụng trên màn hình chính. Nhưng có cách xung quanh, bạn có thể gửi một tải trọng nhỏ cho máy chủ của bạn bất cứ khi nào ứng dụng nhập vào nền trước, sau đó yêu cầu máy chủ gửi thông báo đẩy ngay lập tức.

Also take a look at this : your question is possibly duplicate of it.

Hy vọng điều này sẽ giúp bạn.

+0

bạn có thể vui lòng cho biết cách gửi tải trọng và làm cho máy chủ gửi thông báo đẩy. – arshad

+0

đó là điều bạn phải làm trên máy chủ của riêng bạn với logic của riêng bạn .. nếu bạn không sử dụng một dịch vụ như airship đô thị – Bastian

0

Nói chung, ứng dụng của bạn không được yêu cầu nội dung của thông báo đẩy để hoạt động bình thường. Apple thậm chí không đảm bảo rằng các thông báo đẩy sẽ được chuyển giao (nó sẽ thả tất cả nhưng gần đây nhất nếu thiết bị không có sẵn).

Ứng dụng của bạn phải luôn trao đổi với máy chủ để nhận trạng thái có thẩm quyền về dữ liệu của người dùng (hoặc bất kỳ dữ liệu nào bạn đang trình bày). Nếu bạn nhận được một thông báo đẩy, tất nhiên bạn có thể sử dụng đó như một dấu hiệu để cập nhật hoặc hiển thị thông tin mới. Nhưng ngay cả khi người dùng chạm vào biểu tượng ứng dụng của bạn bình thường (do đó không có thông báo), bạn nên liên hệ với máy chủ để tìm nạp hoặc cập nhật mọi thứ bạn cần.

-1

Mặc dù đây là một câu hỏi cũ, nó xếp hạng cao trong chủ đề này và có giải pháp cho nó như iOS7.

Có một phương pháp được gọi là application:didReceiveRemoteNotification:fetchCompletionHandler: sẽ được gọi ngay cả khi ứng dụng của bạn ở chế độ nền.

Sự cố tôi gặp phải là nó không được gọi. Sau đó tôi tìm thấy this article và nhận ra rằng tôi đã phải bật "Thông báo từ xa" bên trong Khả năng của dự án của tôi để làm việc này.

Hy vọng điều này sẽ hữu ích.

Các vấn đề liên quan