2013-06-27 98 views
4

我已發送Facebook請求。它的工作正常,但我得到的請求(通知)只在iPhone的Facebook應用程序,而不是在Facebook的Web應用程序。我希望Facebook本地應用程序和Web應用程序都能收到通知。我怎樣才能做到這一點?iOS:發送Facebook請求

#pragma Sending Facebook app request 

- (NSDictionary*)parseURLParams:(NSString *)query { 
    NSArray *pairs = [query componentsSeparatedByString:@"&"]; 
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init]; 
    for (NSString *pair in pairs) { 
     NSArray *kv = [pair componentsSeparatedByString:@"="]; 
     NSString *val = 
     [[kv objectAtIndex:1] 
     stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

     [params setObject:val forKey:[kv objectAtIndex:0]]; 
    } 
    return params; 
} 
- (void)sendRequest { 
    NSError *error; 
    NSData *jsonData = [NSJSONSerialization 
         dataWithJSONObject:@{ 
         @"social_karma": @"5", 
         @"badge_of_awesomeness": @"1"} 
         options:0 
         error:&error]; 
    if (!jsonData) { 
     NSLog(@"JSON error: %@", error); 
     return; 
    } 
    NSString *giftStr = [[NSString alloc] 
         initWithData:jsonData 
         encoding:NSUTF8StringEncoding]; 
    NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
            giftStr, @"data", 
            nil]; 

    // Display the requests dialog 
    [FBWebDialogs 
    presentRequestsDialogModallyWithSession:nil 
    message:@"Learn how to make your iOS apps social." 
    title:nil 
    parameters:params 
    handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) { 
     if (error) { 
      // Error launching the dialog or sending the request. 
      NSLog(@"Error sending request."); 
     } else { 
      if (result == FBWebDialogResultDialogNotCompleted) { 
       // User clicked the "x" icon 
       NSLog(@"User canceled request."); 
      } else { 
       // Handle the send request callback 
       NSDictionary *urlParams = [self parseURLParams:[resultURL query]]; 
       if (![urlParams valueForKey:@"request"]) { 
        // User clicked the Cancel button 
        NSLog(@"User canceled request."); 
       } else { 
        // User clicked the Send button 
        NSString *requestID = [urlParams valueForKey:@"request"]; 
        NSLog(@"Request ID: %@", requestID); 
       } 
      } 
     } 
    }]; 
} 

- (void)sendRequestClicked { 
    // Filter and only show friends using iOS 
    [self requestFriendsUsingDevice:@"iOS"]; 
} 


- (void)sendRequest:(NSArray *) targeted { 
    NSError *error; 
    NSData *jsonData = [NSJSONSerialization 
         dataWithJSONObject:@{ 
         @"social_karma": @"5", 
         @"badge_of_awesomeness": @"1"} 
         options:0 
         error:&error]; 
    if (!jsonData) { 
     NSLog(@"JSON error: %@", error); 
     return; 
    } 
    NSString *giftStr = [[NSString alloc] 
         initWithData:jsonData 
         encoding:NSUTF8StringEncoding]; 
    NSMutableDictionary* params = 
    [NSMutableDictionary dictionaryWithObjectsAndKeys:giftStr, @"data", 
    nil]; 

    // Filter and only show targeted friends 
    if (targeted != nil && [targeted count] > 0) { 
     NSString *selectIDsStr = [targeted componentsJoinedByString:@","]; 
     [params setObject:selectIDsStr forKey:@"suggestions"]; 
    } 

    // Display the requests dialog 
    [FBWebDialogs 
    presentRequestsDialogModallyWithSession:nil 
    message:@"Learn how to make your iOS apps social." 
    title:nil 
    parameters:params 
    handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) { 
     if (error) { 
      // Error launching the dialog or sending request. 
      NSLog(@"Error sending request."); 
     } else { 
      if (result == FBWebDialogResultDialogNotCompleted) { 
       // User clicked the "x" icon 
       NSLog(@"User canceled request."); 
      } else { 
       // Handle the send request callback 
       NSDictionary *urlParams = [self parseURLParams:[resultURL query]]; 
       if (![urlParams valueForKey:@"request"]) { 
        // User clicked the Cancel button 
        NSLog(@"User canceled request."); 
       } else { 
        // User clicked the Send button 
        NSString *requestID = [urlParams valueForKey:@"request"]; 
        NSLog(@"Request ID: %@", requestID); 
       } 
      } 
     } 
    }]; 
} 


- (void) requestFriendsUsingDevice:(NSString *)device { 
    NSMutableArray *deviceFilteredFriends = [[NSMutableArray alloc] init]; 
    [FBRequestConnection startWithGraphPath:@"me/friends" 
           parameters:[NSDictionary 
              dictionaryWithObjectsAndKeys: 
              @"id,devices", @"fields", 
              nil] 
           HTTPMethod:nil 
          completionHandler:^(FBRequestConnection *connection, 
               id result, 
               NSError *error) { 
           if (!error) { 
            // Get the result 
            NSArray *resultData = [result objectForKey:@"data"]; 
            // Check we have data 
            if ([resultData count] > 0) { 
             // Loop through the friends returned 
             for (NSDictionary *friendObject in resultData) { 
              // Check if devices info available 
              if ([friendObject objectForKey:@"devices"]) { 
               NSArray *deviceData = [friendObject 
                    objectForKey:@"devices"]; 
               // Loop through list of devices 
               for (NSDictionary *deviceObject in deviceData) { 
                // Check if there is a device match 
                if ([device isEqualToString: 
                 [deviceObject objectForKey:@"os"]]) { 
                 // If there is a match, add it to the list 
                 [deviceFilteredFriends addObject: 
                 [friendObject objectForKey:@"id"]]; 
                 break; 
                } 
               } 
              } 
             } 
            } 
           } 
           // Send request 
           [self sendRequest:deviceFilteredFriends]; 
          }]; 
} 
+0

歡迎使用stackoverflow。從現在開始,當您在SO中發帖時,請考慮以下幾點1)在引用自己時使用'I',而不是'i'。 2)看起來你不是母語爲英語的人,但請確保正確地拆分句子並使用'。'。應有的地方。 3)[這是你如何格式化代碼](http://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks)4)[請不要使用'嗨','謝謝'等標記行在你的帖子](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts) – Krishnabhadra

+0

@克里斯那布哈德謝謝 – VinukondaPraveen

回答

0

如果您的應用程序實施了Facebook Canvas應用程序,則只能在Facebook Web應用程序上獲得通知。

invitable_friends API僅適用於使用Graph API版本2.0的Facebook Canvas應用實現的遊戲 。

Check here the full documentation

Canvas是在把你的應用程序或遊戲直接在 Facebook.com上的臺式機和筆記本電腦的框架。

Details about Canvas

注意:在文檔中,你會發現「你的遊戲」,他們的意思是「您的遊戲或你的應用程序」。