Search Unity

Unity to SMS - Android and Ios

Discussion in 'Scripting' started by OJ3D, Apr 7, 2016.

  1. OJ3D

    OJ3D

    Joined:
    Feb 13, 2014
    Posts:
    33
    I was hunting this down for a while with mix/various results. Tested it today and Got both to successfully work in the latest update of ios (9.3) and Android. This activates your mobile messenger app and pre-fills out the mobile Number, and body fields.

    Hope it helps!
    If you have different methods, please provide your input.
    Cheers!

    //Method-1

    Code (CSharp):
    1.                     string mobile_num = "Your_Number";
    2.             string message ="This is a test from Unity";
    3.  
    4.             #if UNITY_ANDROID  
    5.             string URL = string.Format("sms:{0}?body={1}",mobile_num,message);
    6.             #endif
    7.  
    8.             #if UNITY_IOS  
    9.             string URL = string.Format("sms:{0}?&body={1}",mobile_num,WWW.EscapeURL(message));
    10.             #endif
    11.  
    12.             //Execute Text Message
    13.             Application.OpenURL(URL);
    //Method 2
    This ios version worked to -

    Code (CSharp):
    1. #if UNITY_IOS  
    2.             string URL = string.Format("sms:{0}&body={1}",mobile_num,WWW.EscapeURL(message));
    3.             #endif
     
    Last edited: Apr 7, 2016
  2. OJ3D

    OJ3D

    Joined:
    Feb 13, 2014
    Posts:
    33
    Here's an update.
    So In order for ios and android to work properly it must be encoded, and WWW.Escape isn't a good method to encode. It encode's spaces in your string with "+" and they show up in the body of the text message. Instead use System.Uri.EscapeDataString(Your_String) which properly does it.

    Here's an updated version of what truly works. Tested in ios 9.3 with xcode 7.3, and android 6 - Marshmallow.

    Code (CSharp):
    1.             string mobile_num = "Your_Number";
    2.             string message = "This is a test from Unity *^#$#$((*&& Test Symbols";
    3.  
    4.             #if UNITY_ANDROID
    5.             //Android SMS URL - doesn't require encoding for sms call to work
    6.             string URL = string.Format("sms:{0}?body={1}",mobile_num,System.Uri.EscapeDataString(message));
    7.             #endif
    8.  
    9.             #if UNITY_IOS
    10.             //ios SMS URL - ios requires encoding for sms call to work
    11.             //string URL = string.Format("sms:{0}?&body={1}",mobile_num,WWW.EscapeURL(message)); //Method1 - Works but puts "+" for spaces
    12.             //string URL ="sms:"+mobile_num+"?&body="+WWW.EscapeURL(message); //Method2 - Works but puts "+" for spaces
    13.             //string URL = string.Format("sms:{0}?&body={1}",mobile_num,System.Uri.EscapeDataString(message)); //Method3 - Works perfect
    14.             string URL ="sms:"+mobile_num+"?&body="+ System.Uri.EscapeDataString(message); //Method4 - Works perfectly
    15.             #endif
    16.  
    17.             //Execute Text Message
    18.             Application.OpenURL(URL);
     
    Last edited: Apr 9, 2016
    Baydogan, mimminito and iwunu like this.
  3. iwunu

    iwunu

    Joined:
    Apr 20, 2016
    Posts:
    5
    Thanks.
    Is it possible for winphone?
     
  4. OJ3D

    OJ3D

    Joined:
    Feb 13, 2014
    Posts:
    33
    @iwunu - I'm not completely sure. I don't have a windows phone to test.

    You may want to test either method (ios/android) and remove the "#If PLATFORM_DECLARATION & #endif" and see which method the winphone prefers.
     
  5. iwunu

    iwunu

    Joined:
    Apr 20, 2016
    Posts:
    5
    Tested on winphone 8.1 & 10

    Windows.ApplicationModel.Chat.ChatMessage sms = new Windows.ApplicationModel.Chat.ChatMessage();
    sms.Body = message;
    sms.Recipients.Add(mobile_num);

    UnityEngine.WSA.Application.InvokeOnUIThread(async () =>
    {
    await Windows.ApplicationModel.Chat.ChatMessageManager.ShowComposeSmsMessageAsync(sms);
    }, false);
     
  6. Accerak

    Accerak

    Joined:
    Sep 7, 2017
    Posts:
    1
    I don't understand, i have synced my Android and iphone , but i am not allowed to transfer messages between Android and iPhone,
     
    Last edited: Sep 23, 2017
  7. Andrtyu

    Andrtyu

    Joined:
    Nov 8, 2017
    Posts:
    1
    I don't feel any difficulty, telling me how did you do ? or it could be that the device is not fully connected , maybe you need to root your phone device ,
     
    Last edited: Nov 10, 2017
  8. Anusain_Mv

    Anusain_Mv

    Joined:
    Jan 6, 2017
    Posts:
    4
    Can anyone help me??
    i am making a app in which we receive an OTP and i want to get that it automatically filled in my app.
     
  9. iPedro

    iPedro

    Joined:
    Dec 28, 2011
    Posts:
    14
    Anyone figured out how to add a file attachment using this? I want to send an animated gif as a text message (MMS).
     
  10. Imsoconstipated

    Imsoconstipated

    Joined:
    Nov 10, 2018
    Posts:
    4
    Is there a way to send the message without adding a phone number? I want the player to choose who to send the text to after they click the button. the code your showing works, but when I choose a person to send the text to the body is blank.
     
    hereoofthewild likes this.
  11. Imsoconstipated

    Imsoconstipated

    Joined:
    Nov 10, 2018
    Posts:
    4
    my problem is if messages is open then the code works perfectly. but if messages is closed then the messages app opens but it goes to the home page and my body message is erased.