Advertisement
JohnnyTurbo

NotificationChannelCreator.java

Sep 26th, 2018
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1. package com.JTGD.nofitcation_channel_controller;
  2.  
  3. import android.app.NotificationChannel;
  4. import android.app.NotificationManager;
  5. import android.content.Context;
  6. import android.os.Build;
  7. import android.util.Log;
  8.  
  9. public class NotificationChannelCreator {
  10.     //Creates an instance of the class
  11.     private static final NotificationChannelCreator ourInstance = new NotificationChannelCreator();
  12.  
  13.     //Returns the instance of the class
  14.     public static NotificationChannelCreator getInstance() {
  15.         return ourInstance;
  16.     }
  17.  
  18.     //Constructor, no code necessary here
  19.     private NotificationChannelCreator() {
  20.     }
  21.  
  22.     //Defines our channel IDs. This is what we will enter in our Firebase console
  23.     //to send a message to a specific notification channel.
  24.     public static final String CHANNEL_1_ID = "breaking_news";
  25.     public static final String CHANNEL_2_ID = "daily_deals";
  26.  
  27.     //We will call this function from Unity when we open our application.
  28.     //This will create the notification channels so we can send messages to them.
  29.     private void createNotificationChannels(Context curContext){
  30.  
  31.         //Notification channels only work in Android Oreo or greater,
  32.         //this checks to ensure the device is running Android Oreo or greater
  33.         if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
  34.  
  35.             //Creates a log message. We use the 'Unity' tag so we can see it
  36.             //along side other Unity Debug messages in our device logs.
  37.             Log.i("Unity", "Creating Notification Channels");
  38.  
  39.             //Setting up the first notification channel.
  40.             NotificationChannel channel1 = new NotificationChannel(
  41.                     CHANNEL_1_ID,
  42.                     "Breaking News",
  43.                     NotificationManager.IMPORTANCE_HIGH
  44.             );
  45.             channel1.setDescription("Get the latest news from this app!");
  46.  
  47.             //Setting up the second notification channel.
  48.             NotificationChannel channel2 = new NotificationChannel(
  49.                     CHANNEL_2_ID,
  50.                     "Deals of the Day",
  51.                     NotificationManager.IMPORTANCE_HIGH
  52.             );
  53.             channel2.setDescription("See the latest deals as soon as they are announced!");
  54.  
  55.             //Get the notification manager of the application's context (from Unity)
  56.             NotificationManager nManager = curContext.getSystemService(NotificationManager.class);
  57.  
  58.             //Create the two notification channels
  59.             nManager.createNotificationChannel(channel1);
  60.             nManager.createNotificationChannel(channel2);
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement