Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved UnityWebRequest webRequest not working after importing Firebase [iOS]

Discussion in 'Scripting' started by DE_Hizral, Apr 6, 2022.

  1. DE_Hizral

    DE_Hizral

    Joined:
    Feb 21, 2020
    Posts:
    19
    Hello there,

    Have a few question I would like to asked, base on the title above I am having some issues with my Unity project, I have a simple UnityWebRequest that I did to call URL from inside my app It work just fine in the editor and after I build in in Android and iOS.

    But after I import Firebase into my project and setup Firebase messaging so that I can used push notification I have trouble calling the UnityWebRequst to call URL from my apps. The other problem is right now, this only happen on iOS only, on my Android build I don't have any trouble.

    Been google and looking around the web but I could not find any answer for this issues. Anyone have this kind of problem too? if anyone can assist it would be helpful.

    Below here I put the script that I used to call URL from inside my app :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5. using UnityEngine.UI;
    6. using System.IO;
    7. using System;
    8.  
    9. public class NewsFeed : MonoBehaviour
    10. {
    11.     public string TextureURL = "";
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         Caching.ClearCache();
    17.         UnityWebRequest.ClearCookieCache();
    18.         StartCoroutine(DownloadImage(TextureURL));
    19.     }
    20.  
    21.     IEnumerator DownloadImage(string MediaUrl)
    22.     {
    23.         UnityWebRequest request = UnityWebRequestTexture.GetTexture(MediaUrl);
    24.         yield return request.SendWebRequest();
    25.         if (request.isNetworkError || request.isHttpError)
    26.             Debug.Log(request.error);
    27.         else
    28.         {
    29.             Texture2D webTexture = ((DownloadHandlerTexture)request.downloadHandler).texture as Texture2D;
    30.             Sprite webSprite = SpriteFromTexture2D(webTexture);
    31.             gameObject.GetComponent<Image>().sprite = webSprite;
    32.         }
    33.  
    34.         request.Dispose();
    35.         request = null;
    36.     }
    37.  
    38.     Sprite SpriteFromTexture2D(Texture2D texture)
    39.     {
    40.         return Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), new Vector2(0.5f, 0.5f), 100.0f);
    41.     }
    42. }
    and here is my script that I used for the Firebase messaging:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Firebase.Messaging;
    5.  
    6. public class FirebasePushNot : MonoBehaviour
    7. {
    8.     void Start()
    9.     {
    10.         Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
    11.         Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
    12.     }
    13.  
    14.     public void OnTokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs token)
    15.     {
    16.         Debug.Log("Received Reg Token:" + token.Token);
    17.     }
    18.  
    19.     public void OnMessageReceived(object sender, Firebase.Messaging.MessageReceivedEventArgs e)
    20.     {
    21.         Debug.Log("Received a new Message from:" + e.Message.From);
    22.     }
    23. }
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,637
    What does it mean that it "doesn't work"?
    Does is error out, or does it get stuck?
     
  3. DE_Hizral

    DE_Hizral

    Joined:
    Feb 21, 2020
    Posts:
    19
    I receive no error from the editor, and when I tried to build on Xcode it build without any problem. The app run after build, but the webrequest seem to not calling the texture is suppose to called from the URL provided.

    If I remove the firebase package/plugin and build again on Xcode and run the app, the texture is called and load from the URL provided.

    So I'm not sure why Firebase is effecting the texture called from URL.
     
  4. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Place additional Debug.Log calls to check to ensure the code you expect is indeed running. Also, you can check the network request using Charles Proxy to compare the differences https://support.unity.com/hc/en-us/articles/115002917683-Using-Charles-Proxy-with-Unity
     
  5. DE_Hizral

    DE_Hizral

    Joined:
    Feb 21, 2020
    Posts:
    19
    Thank you for the reply, and sorry for the late reply.

    I've manage to sort this one out, apparently I have to change my "http://" to "https://" for the URL link. It work now.
    But what puzzle me was before I put Firebase the "http://" work just fine, I don't need to change it to "https://".

    So the URL work fine for Firebase if I change it to "https://".

    Thank you for your time.
     
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,637
    That's very strange.
    On iOS there is a setting to enable http, maybe Firebase integration removes it?