2014-11-04 129 views
-2

我想弄清楚如何檢查推送通知的有效負載,以確定當用戶從通知中打開應用程序時打開哪個視圖。例如,如果通知顯示「x:test」視圖x將在通知被點擊時打開,並且通知顯示「y:test」視圖將會打開。檢查iOS推送通知有效負載

編輯:我想我應該澄清我不確定的部分。

我有這樣的didFinishLaunchingWithOptions:

UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; 

    if (notification) 
    { 
     // check payload then load appropriate view controller 
    } 

如何我會檢查某些文本的有效載荷,以確定相應的視圖控制器加載?

+0

您是否做過任何搜索?在AppDelegate.m中的應用程序didFinishLaunchingWithOptions方法中,您可以閱讀遠程通知,並根據if語句「說」的內容打開正確的視圖。 – Kostis 2014-11-04 20:23:45

+0

https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction.html看來你正在嘗試做類似於http://stackoverflow.com/questions/中的解決方案8685333 /如何管理通知當用戶點擊徽章 – propstm 2014-11-04 20:50:45

回答

1

這是我過去的項目的代碼。通知顯示在設備中,例如「Kostas:想加你爲朋友」。

[[UIApplication sharedApplication] registerForRemoteNotificationTypes: 
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; 

NSDictionary *remoteNotif = 
[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; 

if (remoteNotif) { 

    NSString *name = [[NSString alloc] init]; 
    NSString *message = [[NSString alloc] init]; 
    // 'wants to add you as friend.' 
    NSString* alertValue = [[remoteNotif valueForKey:@"aps"] valueForKey:@"alert"]; 

    NSMutableArray* parts = [NSMutableArray arrayWithArray:[alertValue componentsSeparatedByString:@": "]]; 
    name = [parts objectAtIndex:0]; 
    [parts removeObjectAtIndex:0]; 
    message = [parts componentsJoinedByString:@": "]; 

    if ([message isEqualToString:@": wants to add you as friend."]) { 
     UITabBarController *tabController = (UITabBarController *)self.window.rootViewController; 
     // tabController.delegate = self; 
     tabController.selectedIndex = 1; 
    } 
    else{ 
     UITabBarController *tabController = (UITabBarController *)self.window.rootViewController; 
     // tabController.delegate = self; 
     tabController.selectedIndex = 2; 

     [self addMessageFromRemoteNotification:remoteNotif updateUI:NO]; 
    } 
1

接收推送通知是在兩個地方在應用程序委託

  • - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions當你的應用程序從一個推送通知啓動處理。在這種情況下,您的推送通知數據包含在[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]

  • - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo當您的應用在運行時收到通知。在這種情況下,userInfo是您的推送通知數據。

+0

請參閱我的更新。我澄清了我不確定的事情。 – raginggoat 2014-11-04 20:45:41