Why can't I send a private message from one user to another using Photon Chat

ChethanV007
edited February 2022 in Photon Chat

Hi I am trying to send a private message from one friend to another friend using Photon Chat. But for some reason I am not able to do. Rather I am getting the message what I am sending to myself.

So this is my Script which sets ups and controls my PhotonChat:

public class PhotonChatController : MonoBehaviour, IChatClientListener
{
    [SerializeField] private string nickname;
    [SerializeField] private Text Status;
    private ChatClient chatclient;

    private void Awake()
    {
        nickname = PlayerPrefs.GetString("USERNAME");
        FriendBoxManager.SendInviteEvent += SendInvite;       
    }
    private void OnDestroy()
    {
        FriendBoxManager.SendInviteEvent -= SendInvite;        
    }

    void Start()
    {
       Application.runInBackground=true;
        if (string.IsNullOrEmpty(PhotonNetwork.PhotonServerSettings.AppSettings.AppIdChat))
           {
               Debug.Log("no CHat app ID provided");
               return;
           }
        chatclient = new ChatClient(this);
        ConnectToPhotonChat();
    }

    private void ConnectToPhotonChat()
    {      
         chatclient.ChatRegion="ASIA";
         chatclient.Connect(PhotonNetwork.PhotonServerSettings.AppSettings.AppIdChat,PhotonNetwork.AppVersion,
         new Photon.Chat.AuthenticationValues(nickname));               
      } 
 
    private void SendInvite(string recepientFriend)
    {        
        chatclient.SendPrivateMessage(recepientFriend, "INVITE");
        Debug.Log("Sending message to "+recepientFriend);       
    }

    void Update()
    {    
      chatclient.Service();
    }

 public void OnPrivateMessage(string sender, object message, string channelName)
    {      
        if (!string.IsNullOrEmpty(message.ToString()))
             {
                  //channel name is the format [sender : recepient]
                 string[] splitnames = channelName.Split(new char[] { ':' }); //   here you are seperatign the sender and receiver names
                 string senderName = splitnames[0];
                 Debug.Log("Outside");
                 Status.text="Outside";
                 if (!sender.Equals(senderName, StringComparison.OrdinalIgnoreCase))
                 {
                     Debug.Log("inside........");
                     Debug.Log($"{sender}:{message}");
                     Status.text=$"{sender}:{message}";
                 }
             }
           
    }


So briefly explaining the above code the nickname is the USERNAME stored and that is the AuthenticationValues as well while establishing the connection. In the SendInvite listener basically I am sending the private message.Actually the Status.text is a simple UI text , wherein the word "Outside" is shown only on my screen and not on the friends screen

Explaining from the beginning, a list of Playfab friends is found and then the from that list of friends PhotonNetwork.FindFriends(friendDisplayNamesList) is exectued. This function is repeatedly called for every five seconds actually.That Inturn executes this call back function

 public override void OnFriendListUpdate(List<PhotonFriendInfo> friendList)
    {
        base.OnFriendListUpdate(friendList);       
        DisplayFriends?.Invoke(friendList);        
    }

and in display friends the UI is updated wherein the friends name is :

FriendName.text = friend.UserId; 

In the invite friend event this is what is invoked:

 public void SendInvite()
    {
        Debug.Log("FriendNameUserID........"+FriendName.text);
        SendInviteEvent?.Invoke(FriendName.text);// the listener is in Photon Chat Controller       
    }

I really don't understand where am I going wrong.....

Answers

  • JohnTube
    JohnTube ✭✭✭✭✭

    Hi @ChethanV007,

    A client that sends a message in Photon Chat will also receive it, that's expected.

    Make sure PlayFab UserID match Photon Chat UserID.

    Make sure both clients are on the same region, using the same AppId and same AppVersion.

    First test with both clients connected, then test that client who should receive the message is not and as soon as connected (and sender is still connected) the message will be received.

    Photon Chat keeps up to the last 100 private messages as longs as one of the two ends of the private conversation is connected (or until both ends of private conversation are disconnected).

  • ChethanV007
    edited February 2022

    Honestly speaking I just now tested it on two mobile devices and the code works. It is only that when One of them is a mobile device and the other is a Unity editor , it doesn't work.

    I just got this random doubt and thats it, that doubt was correct