2012-01-18 55 views
3

是否可以使用NSDictionary從發送的推送通知中檢索信息? (例如,獲取警報有效載荷包含的標題,消息和聲音)。iOS 5使用NSDictionary從發送的推送通知中檢索信息

我也想發送有效負載中的信息(比如一個字符串),以便使用與標題或消息無關的應用程序。再次,這可能嗎?

回答

8

是的,無論是可能的!

參照獲取所期望的信息,執行下列操作:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{  
    // Push notification was received when the app was in the background 

    // ..... 
    if (launchOptions != nil) 
    { 
     NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; 
     if (dictionary != nil) 
     { 
      NSLog(@"Launched from push notification: %@", dictionary); 
      // do something with your dictionary 
     } 
    } 
    // ..... 
    return YES; 
} 

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo 
{ 
    // Push notification received while the app is running 

    NSLog(@"Received notification: %@", userInfo); 
    // do something with your dictionary 
} 
2

是的,你可以得到這些信息。在userInfo NSDictionary實例的內部,在關鍵字aps下有一個屬性(其中包含另一個NSDictionary)。這包含警報,徽章和聲音鍵的其他屬性。

傳遞的自定義信息將出現在userInfo NSDictionary實例中,在發送推送通知時所提供的參數下。

更多信息,請參見UIApplicationDelegate協議參考:http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html