2009-10-12 104 views
27

如果應用程序已運行,我們如何處理推送通知?如果應用程序正在運行(而不是推送通知警報),我想顯示警報。只有在應用程序未運行時,才顯示推送通知警報。如果應用程序已在運行,如何處理推送通知?

此外,如果我向APN發送有效載荷,如何使用取消按鈕創建警報?

回答

52

您可以實現application:didReceiveRemoteNotification:

這裏是一個可能的示例代碼:

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

謝謝。 Apple對此很不方便。 – DGund 2013-01-17 02:54:39

+1

@DGund真的嗎?如果您嘗試編寫Android的推送(GCM/C2DM),您會發現它更加複雜。 – Raptor 2013-08-09 07:57:08

+1

@notnoop代碼有問題。應該用'if(message)'替換'if(alert)' – Raptor 2013-08-09 08:29:05

14

「警報」鍵不會在那裏直屬用戶信息字典,你需要得到另一個字典名稱爲「APS 「然後從」aps「字典中獲得」alert「或」body「。

45

您可以檢查UIApplication的狀態。只要做到這樣

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

很好的答案,謝謝。 – filou 2012-05-20 23:41:49

+0

'yourMessage'沒有被定義。您至少應該說明如何從'userInfo'獲取消息。 – Raptor 2013-08-09 08:22:55

+0

@ShivanRaptor - 我不認爲這是問題的關鍵? 這是做這件事的好方法 - 不是像檢查userInfo是字符串還是字典那樣的破解。 – Geebs 2013-08-18 03:17:27

5

迭代檢查的3個級別的有效載荷

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

    } 
    } 

}

結果是:

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 
相關問題