我使用 facebook api 连接到 facebook 并通过 api 提供的本机对话框发送请求。
我按照developers.facebook.com上的文档中发布的示例但是我在发送请求时遇到了以下问题:1.请求未显示在通知中 - 仅在应用程序中心 - 在这种情况下,我认为这是一个问题应用程序在沙盒中,未发布到 APPSTORE
- 我成功地使用正确的 fbUser id 向 facebook 服务器发送请求。但是当我想在应用程序中接收通知时,问题就来了:
按照授权用户的文档,我应该在 open url 方法中设置这个:
fb[APP_ID]://authorize#expires_in=[ACCESS_TOKEN_EXPIRATION]
&access_token=[USER_ACCESS_TOKEN]
&target_url=https://apps.facebook.com/[APP_NAME_SPACE]/?request_ids=
[COMMA_SEPARATED_REQUESTIDs]&ref=notif&app_request_type=user_to_user
但我只能看到没有目标网址的普通登录……我可以看到会话到期日期、fb 应用程序 ID、访问令牌等。但是没有目标网址?
那么基本上target_url是什么?应该怎么设置?发送请求时我必须包括什么?
另外:应用程序句柄打开url方法被正确调用。应用程序激活后,也会正确调用 checkRequests 方法。
请不要将我链接到文档。我已经阅读了 50 遍,但没有找到任何合理的解决方案......
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
// attempt to extract a token from the url
self.openedURL = url;
NSLog(@"%@",url);
return [FBSession.activeSession handleOpenURL:url];
}
- (void)sendRequest {
FBSBJSON *jsonWriter = [FBSBJSON new];
NSDictionary *gift = [NSDictionary dictionaryWithObjectsAndKeys:
@"5", @"points",
@"1", @"badge",
nil];
NSString *giftStr = [jsonWriter stringWithObject:gift];
NSMutableDictionary* params =
[NSMutableDictionary dictionaryWithObjectsAndKeys:
@"Hi from test app", @"message",
giftStr, @"data",
nil];
[self.facebook dialog:@"apprequests"
andParams:params
andDelegate:self];
}
// Handle the request call back
- (void)dialogCompleteWithUrl:(NSURL *)url {
NSDictionary *params = [self parseURLParams:[url query]];
NSString *requestID = [params valueForKey:@"request"];
NSLog(@"Request ID: %@", requestID);
}
-(FBSession*)returnSession{
return self.session;
}
/*
* Helper function to get the request data
*/
- (void) notificationGet:(NSString *)requestid {
[FBRequestConnection startWithGraphPath:requestid
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
if (!error) {
NSString *title;
NSString *message;
if ([result objectForKey:@"data"]) {
title = [NSString
stringWithFormat:@"%@ sent you a gift",
[[result objectForKey:@"from"]
objectForKey:@"name"]];
FBSBJSON *jsonParser = [FBSBJSON new];
NSDictionary *requestData =
[jsonParser
objectWithString:[result objectForKey:@"data"]];
message =
[NSString stringWithFormat:@"Badge: %@, Karma: %@",
[requestData objectForKey:@"badge"],
[requestData objectForKey:@"points"]];
} else {
title = [NSString
stringWithFormat:@"%@ sent you a request",
[[result objectForKey:@"from"] objectForKey:@"name"]];
message = [NSString stringWithString:
[result objectForKey:@"message"]];
}
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil,
nil];
[alert show];
// Delete the request notification
[self notificationClear:[result objectForKey:@"id"]];
}
}];
}
/*
* Helper function to check incoming URL
*/
- (void) checkIncomingNotification {
if (self.openedURL) {
NSString *query = [self.openedURL fragment];
if (!query) {
query = [self.openedURL query];
}
NSDictionary *params = [self parseURLParams:query];
for (NSString * str in [params allKeys]) {
NSLog(@"key %@", str);
}
// Check target URL exists
NSString *targetURLString = [params valueForKey:@"target_url"];
if (targetURLString) {
NSURL *targetURL = [NSURL URLWithString:targetURLString];
NSDictionary *targetParams = [self parseURLParams:[targetURL query]];
NSString *ref = [targetParams valueForKey:@"ref"];
// Check for the ref parameter to check if this is one of
// our incoming news feed link, otherwise it can be an
// an attribution link
if ([ref isEqualToString:@"notif"]) {
// Get the request id
NSString *requestIDParam = [targetParams
objectForKey:@"request_ids"];
NSArray *requestIDs = [requestIDParam
componentsSeparatedByString:@","];
// Get the request data from a Graph API call to the
// request id endpoint
[self notificationGet:[requestIDs objectAtIndex:0]];
}
}
// Clean out to avoid duplicate calls
self.openedURL = nil;
}
}
有没有什么办法可以说明这些问题是由应用程序未在 Appstore 上发布的方式引起的(iPhone 和 iPad 都没有设置 Appstore id)?
以下是显示使用 fb api 的代码片段:
非常感谢您的时间。