27

Làm cách nào để chúng tôi xử lý thông báo đẩy nếu ứng dụng là đã hoạt động? Tôi muốn hiển thị cảnh báo nếu ứng dụng đang chạy (thay vì cảnh báo thông báo đẩy). Chỉ khi ứng dụng không chạy, sau đó hiển thị cảnh báo thông báo đẩy.Làm thế nào để xử lý các thông báo đẩy nếu ứng dụng đang chạy?

Ngoài ra, nếu tôi gửi tải trọng cho APN, làm cách nào để tạo cảnh báo bằng nút hủy?

Trả lời

52

Bạn có thể thực hiện application:didReceiveRemoteNotification:

Dưới đây là một mẫu mã có thể:

- (void)application:(UIApplication *)application 
    didReceiveRemoteNotification:(NSDictionary *)userInfo 
{ 
    NSString *message = nil; 
    id alert = [userInfo objectForKey:@"alert"]; 
    if ([alert isKindOfClass:[NSString class]]) { 
    message = alert; 
    } else if ([alert isKindOfClass:[NSDictionary class]]) { 
    message = [alert objectForKey:@"body"]; 
    } 
    if (alert) { 
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title" 
             message:@"AThe message." delegate:self 
          cancelButtonTitle:@"button 1" 
          otherButtonTitles:@"button", nil]; 
    [alertView show]; 
    [alertView release]; 
    } 
+1

THANK YOU. Apple đã thực sự bất tiện về điều này. – DGund

+1

@DGund thực sự? Nếu bạn cố gắng viết tính năng đẩy của Android (GCM/C2DM), bạn sẽ thấy nó phức tạp hơn nhiều. – Raptor

+1

@notnoop Mã có vấn đề. Nên thay thế 'if (alert)' bằng 'if (message)' – Raptor

14

"cảnh báo" chìa khóa sẽ không có mặt ở đó trực thuộc từ điển UserInfo, bạn cần phải nhận được từ điển khác với tên "aps "và sau đó nhận được" cảnh báo "hoặc" cơ thể "từ" aps "từ điển.

45

Bạn có thể kiểm tra trạng thái của UIApplication. Chỉ cần làm một kiểm tra như thế này

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{ 

    UIApplicationState state = [application applicationState]; 
    if (state == UIApplicationStateActive) 
    { 

      UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"xxx" message:yourMessage delegate:self cancelButtonTitle:@"Done" otherButtonTitles: @"Anzeigen", nil] autorelease]; 
      [alert setTag: 2]; 
      [alert show]; 
    } 
    else { 
     // Push Notification received in the background 
    } 
} 
+0

Câu trả lời hay, cảm ơn bạn. – filou

+0

'yourMessage' không được xác định. Bạn nên ít nhất minh họa cách lấy thông báo từ 'userInfo'. – Raptor

+0

@ShivanRaptor - Tôi không nghĩ đó là điểm của câu hỏi? Đây là cách tốt hơn để làm điều đó - không phải là hack như kiểm tra nếu userInfo là một chuỗi hoặc một từ điển. – Geebs

5

lặp lại 3 mức tải trọng

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { 

for (id key in userInfo) { 
    NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]); 
    NSString *message = nil; 

NSDictionary *aps = [NSDictionary dictionaryWithDictionary:(NSDictionary *) [userInfo objectForKey:key] ]; 
    for (id key1 in aps){ 
     NSLog(@"key1: %@", key1); 
     id alert = [aps objectForKey:key1]; 
     if ([alert isKindOfClass:[NSDictionary class]]) { 
      message = [alert objectForKey:@"body"]; 
      NSLog(@"body: %@, value: %@", key1, message); 
      message = [alert objectForKey:@"loc-args"]; 
      NSLog(@"loc-args: %@, value: %@", key1, message); 
      NSArray *args = (NSArray *) [alert objectForKey:@"loc-args"] ; 
       for (id key2 in args){ 
        NSLog(@"key2: %@, value: ", key2); 
       } 
      message = [alert objectForKey:@"action-loc-key"]; 
      NSLog(@"action-loc-key: %@, value: %@", key1, message); 

     } 
     else if ([alert isKindOfClass:[NSArray class]]) { 
      for (id key2 in key1){ 
       NSLog(@"key2: %@, value: %@", key2, [key1 objectForKey:key2]); 
      } 
     } 
     else if([key1 isKindOfClass:[NSString class]]) { 
      message = [aps objectForKey:key1]; 
      NSLog(@"key1: %@, value: %@", key1, message); 
     } 

    } 
    } 

}

Kết quả là:

2012-01-27 20:38:09.599 SPush[4181:707] key: aps, value: { 
alert =  { 
    "action-loc-key" = Open; 
    body = test; 
    "loc-args" =   (
     1000, 
     2000 
    ); 
}; 
badge = 0; 
"content-available" = 10; 
sound = default; 
} 
2012-01-27 20:38:13.133 SPush[4181:707] key1: alert 
2012-01-27 20:38:13.134 SPush[4181:707] body: alert, value: test 
2012-01-27 20:38:13.137 SPush[4181:707] loc-args: alert, value: (
1000, 
2000 
) 
2012-01-27 20:38:13.138 SPush[4181:707] key2: 1000, value: 
2012-01-27 20:38:13.139 SPush[4181:707] key2: 2000, value: 
2012-01-27 20:38:13.140 SPush[4181:707] action-loc-key: alert, value: Open 
2012-01-27 20:38:13.141 SPush[4181:707] key1: sound 
2012-01-27 20:38:13.143 SPush[4181:707] key1: sound, value: default 
2012-01-27 20:38:13.144 SPush[4181:707] key1: badge 
2012-01-27 20:38:13.145 SPush[4181:707] key1: badge, value: 0 
2012-01-27 20:38:13.146 SPush[4181:707] key1: content-available 
2012-01-27 20:38:13.147 SPush[4181:707] key1: content-available, value: 10 
Các vấn đề liên quan