简体   繁体   中英

Facebook Messenger SDK to share text to a specific user

All I want to do is have my app link into FB messenger so the user can send a message to their friend. I have the Fb Id of all my users, I figured I could use that to open messenger to the user's thread.

The SDK only provides ways of sending media, but not a way to allow the user to send plain text. How can I allow a user to send a message through messenger to their friend from my app?

I tried this

UIImage *image = [UIImage imageNamed:@"test.png"];
[FBSDKMessengerSharer shareImage:image withOptions:nil];

And it works as expected; it pops over to messenger with the image ready to go and lets me select users and allows me to enter more text.

How can I achieve the same functionality without needing to send over media? (using nil as the image did not work).

And how can I use a call like this to pre-populate the receiving user based on a FB ID?

The SDKs do not support starting a plain conversation between two users without some sort of content (image, video, link etc.) being shared and the message has to be entered manually, character by character by the user anyways as pre-filling is not allowed on the platform.

You can check the documentation for the Send Button on iOS or the docs for Messenger Platform to check all the options you have.

Facebook Messenger SDK to share text to a specific user

-(void)messengerclicked {

NSLog(@"messenger tapped");

NSString * urlWhats = [NSString stringWithFormat:@"fb-messenger://user-thread/ENTER NUMERIC ID OF FACEBOOK PROFILE/"];

NSURL * facebookURL = [NSURL URLWithString:[urlWhats stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
if ([[UIApplication sharedApplication] canOpenURL: facebookURL]) {
    [[UIApplication sharedApplication] openURL: facebookURL];
} else {
    // Cannot open facebook
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"brand name"
                                                                   message:@"Fb-Messenger is not installed. \n Press Ok to Download and Install "
                                                            preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"OK"
                                                       style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction *action)
                               {
                                   NSString *iTunesLink = @"itms://itunes.apple.com/us/app/messenger/id454638411?mt=8";
                                   [[UIApplication sharedApplication] openURL:[NSURL URLWithString:iTunesLink]];

                               }];

    //You can use a block here to handle a press on this button
    [alert addAction:actionOk];
    [self presentViewController:alert animated:YES completion:nil];
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM