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 Android Notifications not being sent using code directly from the docs

Discussion in 'Scripting' started by WhipJr, Apr 14, 2021.

  1. WhipJr

    WhipJr

    Joined:
    Aug 3, 2011
    Posts:
    125
    Hey everyone!

    It seems as though something is wrong with either my code (copied directly from the docs) or in the backend of AndroidNotificationCenter.

    When I run the app on a phone it only ever displays up to my "Notification Created" debug line.

    Code (CSharp):
    1. using Unity.Notifications.Android;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class Notif : MonoBehaviour
    6. {
    7.     public Text inAppDebug;
    8.  
    9.     private void OnApplicationPause(bool pause)
    10.     {
    11.         inAppDebug.text = "";
    12.  
    13.         DebugText("Starting Notification Process");
    14.         var channel = new AndroidNotificationChannel()
    15.         {
    16.             Id = "channel_id",
    17.             Name = "Default Channel",
    18.             Importance = Importance.Default,
    19.             Description = "Generic notifications",
    20.         };
    21.  
    22.         AndroidNotificationCenter.RegisterNotificationChannel(channel);
    23.         DebugText("Channel Created");
    24.  
    25.         var notification = new AndroidNotification();
    26.         notification.Title = "Your Title";
    27.         notification.Text = "Your Text";
    28.         notification.FireTime = System.DateTime.Now.AddSeconds(15);
    29.         DebugText("Notification Created");
    30.  
    31.         var id = AndroidNotificationCenter.SendNotification(notification, "channel_id");
    32.         DebugText("Notification Sent");
    33.  
    34.     }
    35.     public void DebugText(string s)
    36.     {
    37.         inAppDebug.text += $"\n{s}";
    38.     }
    39. }

    Any ideas? I'm working in Unity 2019.3.0b8 with Mobile Notifications Package 1.0.3
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Time to bust out the
    adb logcat
    and see if there's any clues in there.

    Just in general for this class of issue, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
  3. WhipJr

    WhipJr

    Joined:
    Aug 3, 2011
    Posts:
    125
    Thats what the DebugText function is for.. i have the info printing to a text object on the phone so i can see where its stopping.

    Didn't know about adb logcat though ill check there and see if what I find. Thank you!
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    It might give you things like permission violations or who knows what else.
     
  5. WhipJr

    WhipJr

    Joined:
    Aug 3, 2011
    Posts:
    125
    So I never got around to adb logcat... turns out the version of unity which i was using was a beta version i installed a while back and never got around to uninstalling it.

    After moving the project to a newer LTS version it works fine. I I might not have noticed if I was able to use the adb command though. I tried to run it but it wasnt recognized so I was about to install the default android setup for unity and noticed it said BETA on the version i was using lol

    Thank you for steering me in the right direction! ;)
     
    Kurt-Dekker likes this.