Search code examples
c#unity-game-enginephotonplayfab

Unity & Playfab Sending friend requests to offline players


public void SaveFriendRequest(string targetPlayerId, string senderDisplayName)
{
    var data = new Dictionary<string, string>
    {
        { senderDisplayName, "pending" } // "pending" indicates that the request is pending
    };

    var request = new UpdateUserDataRequest
    {
        PlayFabId = targetPlayerId, // Use the recipient's PlayFab ID here
        Data = data,
        Permission = UserDataPermission.Public // or Private, depending on your needs
    };

    PlayFabClientAPI.UpdateUserData(request, OnDataUpdateSuccess, OnDataUpdateFailure);
}

Hey guys! I am dealing with sending membership requests in my game and I am having a problem with the function that is supposed to save the membership request sent to the player. It is not working now and there is even an ERROR: 'UpdateUserDataRequest' does not contain a definition for 'PlayFabId' Maybe the function should be written differently?

Could someone please guide me how exactly do I change something for a player who is not me? Like saving the friend request I sent him while he was not connected to the game.


Solution

  • public void SaveFriendRequest(string targetPlayerId)
    {
        var request = new ExecuteCloudScriptRequest
        {
            FunctionName = "SaveFriendRequest",
            FunctionParameter = new Dictionary<string, object>
            {
                { "targetPlayerId", targetPlayerId }
            },
            GeneratePlayStreamEvent = true
        };
    
        PlayFabClientAPI.ExecuteCloudScript(request, OnCloudScriptSuccess, OnCloudScriptFailure);
    }
    

    Well, then I solved the problem by adding the SaveFriendRequest function in Cloud Script in Playfab:

    handlers.SaveFriendRequest = function (args, context) {
        var targetPlayerId = args.targetPlayerId;
        var senderPlayerId = currentPlayerId; 
    
        var playerData = server.GetUserData({
            PlayFabId: targetPlayerId,
            Keys: ["FriendRequests"] 
        });
    
        var friendRequests = [];
        if (playerData.Data && playerData.Data.FriendRequests) {
            friendRequests = JSON.parse(playerData.Data.FriendRequests.Value);
        }
    
        friendRequests.push(senderPlayerId);
    
        server.UpdateUserData({
            PlayFabId: targetPlayerId,
            Data: {
                "FriendRequests": JSON.stringify(friendRequests)
            }
        });
    
        return { message: "Friend request saved successfully!" };
    };