Advertisement
JohnnyTurbo

CreateNotificationChannels.cs

Sep 26th, 2018
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.25 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CreateNotificationChannels : MonoBehaviour {
  6.  
  7.     const string pluginName = "com.JTGD.nofitcation_channel_controller.NotificationChannelCreator";
  8.     const string unityPlayerName = "com.unity3d.player.UnityPlayer";
  9.  
  10.     private void Start()
  11.     {
  12.         //Check to make sure we are running on
  13.         if (Application.platform == RuntimePlatform.Android)
  14.         {
  15.             //Find the main Unity player class
  16.             AndroidJavaClass unityPlayerClass = new AndroidJavaClass(unityPlayerName);
  17.             if(unityPlayerClass == null)
  18.             {
  19.                 Debug.LogError("Could not find Unity player class named: " + unityPlayerName);
  20.                 return;
  21.             }
  22.  
  23.             //Get the current context of the main Unity player class
  24.             AndroidJavaObject unityPlayerContext = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");
  25.             if(unityPlayerContext == null)
  26.             {
  27.                 Debug.LogError("Could not get the current context of the Unity player class: " + unityPlayerName);
  28.                 return;
  29.             }
  30.  
  31.             //Find the Android notification class
  32.             AndroidJavaClass androidNotificationClass = new AndroidJavaClass(pluginName);
  33.             if (androidNotificationClass == null)
  34.             {
  35.                 Debug.LogError("Could not find plugin class named: " + pluginName);
  36.                 return;
  37.             }
  38.  
  39.             //Get the singleton instance of the Android notification class
  40.             AndroidJavaObject pluginObject = androidNotificationClass.CallStatic<AndroidJavaObject>("getInstance");
  41.             if (pluginObject == null)
  42.             {
  43.                 Debug.LogError("Could not get instance of the plugin: " + pluginName);
  44.                 return;
  45.             }
  46.  
  47.             /* Call the createNotificationChannels() function on the singleton instance of the
  48.              * Android notification class and pass in the application's current context.
  49.              * This will create the notification channels for our application.
  50.              */
  51.             pluginObject.Call("createNotificationChannels", unityPlayerContext);
  52.         }
  53.     }    
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement