Your code won't work if you and the friend you're trying to invite are playing on different platforms. While Android, iOS and MacOS players can add each other to friend lists in the game, they can't do the same with Windows players. For technical reasons Windows players' data is stored on different servers, so they can only add friends who use the same platform.