Android Ultimate Plugin Tutorial part 9 – Local Notification

Hi Guys, if you haven’t try our Local Notification feature on Android Ultimate Plugin here’s some tutorial,  I know you wanted to change the icon on that notification and I also know that you wanted to customize that notification alert sound and you are lucky we are going to this discuss it right now.

add this permission on your Android manifest file before <application> tag

<uses-permission android:name="android.permission.GET_TASKS"/>

then add this line at the end of  main </activity>  tag  and before </application> tag

<receiver android:name="com.gigadrillgames.androidplugin.notification.NotificationPublisher" />

this will received your local notification event when activated. without this no notification will show.

Note:  this tutorial is outdated and Android Ultimate Plugin Controller does not exist anymore.

Let’s start coding, First thing to do get the instance of AndroidUltimatePluginController

Old way

private AndroidUltimatePluginController androidUltimatePluginController = AndroidUltimatePluginController.GetInstance();

New way

First thing to do get the instance of LocalNotificationPlugin

private LocalNotificationPlugin localNotificationPlugin;
localNotificationPlugin = LocalNotificationPlugin.GetInstance();

 

Next we set SetDebug Method to 0 because we don’t want to see any Debug Toast Messages

Old way

androidUltimatePluginController.SetDebug(0);

New way

localNotificationPlugin.SetDebug(0);

And to set a Local Notification we call ScheduleNotification() method and pass corresponding values which is the following

Old way

string notificationTitle="my notification title";
string notificationMessage=,"my notification message";
string notificationTickerMessage="my notification ticker message";
int delay = 3000;
bool enableVibrate = true;
bool enableSound = true;

androidUltimatePluginController.ScheduleNotification(
           notificationTitle
          ,notificationMessage
          ,notificationTickerMessage
          ,delay
          ,enableVibrate
          ,enableSound
);

Then wait for 3000 milliseconds or 3 seconds

New way

public void ScheduleLocalNotification(){
		Debug.Log("ScheduleLocalNotification");
		//schedule notification
		//1000 = 1 sec
		int delay = 3000;
		//request code is the unique id of local notification
		int requestCode = notificationRequestCodeCollection.Count;
		localNotificationPlugin.ScheduleNotification("my notification title","my notification message","my notification ticker message",delay,true,true,requestCode);
		notificationRequestCodeCollection.Add(requestCode);
		
		Debug.Log("added scheduled notification with requestCode " + requestCode );
	}

 

Note: if we set the debug mode to 1 notification will be triggered even if our application is currently running. if the debug mode is 0 local notification will not be triggered if our application is currently running.

to test Local Notification when Debug is 0, we need to activate Local Notification then quit or close our application and wait for Local Notification to be triggered after 3 seconds.

After testing and we confirm that it is working perfectly now is the time to change the icon of Local Notification, you can do this by going to “Assets/Plugins/Android/res” and look for the folder drawable-hdpi, drawable-ldpi, drawable-mdpi, drawable-xhdpi,drawable-xxhdpi and replace the ic_launcher.png by your own customize png image

localNotification_tut_1

Note: make sure that the filename of the new  png  is “ic_launcher” don’t rename it. if you rename it , it will not work or it can produce a bug or your app might stop.

localNotification_tut_2

then after that if you wanted to change the alert sound just replace the alert.wav files with you own wav files this file can be found at “Assets/Plugins/Android/res/raw”

localNotification_tut_3

Note: make sure that the filename of the new wav is “alert” don’t rename it. if you rename it , it will not work or it can produce a bug or your app might stop.

And that’s it, it should be working now and I Hope that you are happy with your new notification icon and notification alert sound.

Note: don’t forget to place android-support-v4.jar on “Assets/Plugins/Android” local notification required it to make it work.

For Reference Here’s the code that we used on Local Notification Demo and Noticed this code has been changed to show the new version on how to used it.

using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;

public class LocalNotificationDemo : MonoBehaviour{

	private LocalNotificationPlugin localNotificationPlugin;

	//store request code of all local notification
	//tip save this on playerpref so that you can still access it when your player quit and the open your application
	private List<int> notificationRequestCodeCollection = new List<int>();
	
	// Use this for initialization
	void Start (){
		localNotificationPlugin = LocalNotificationPlugin.GetInstance();
		localNotificationPlugin.SetDebug(1);
		
		int isOpenUsingNotification = localNotificationPlugin.IsOpenUsingNotification();
		Debug.Log("[ShareAndExperienceDemo]: isOpenUsingNotification " + isOpenUsingNotification);
		
		if(isOpenUsingNotification == 1){
			//do something here
		}else{
			//do something here
		}
	}
	
	public void ScheduleLocalNotification(){
		Debug.Log("ScheduleLocalNotification");
		//schedule notification
		//1000 = 1 sec
		int delay = 3000;
		//request code is the unique id of local notification
		int requestCode = notificationRequestCodeCollection.Count;
		localNotificationPlugin.ScheduleNotification("my notification title","my notification message","my notification ticker message",delay,true,true,requestCode);
		notificationRequestCodeCollection.Add(requestCode);
		
		Debug.Log("added scheduled notification with requestCode " + requestCode );
	}

	public void ScheduleMultipleNotification(){
		Debug.Log("ScheduleMultipleNotification");

		int notificationCount = 3;

		for(int index=0;index<notificationCount;index++){
			//schedule notification
			//1000 = 1 sec
			int delay = 3000 + (index * 3000);
			//request code is the unique id of local notification
			int requestCode = notificationRequestCodeCollection.Count;
			localNotificationPlugin.ScheduleNotification("my notification title_" + index,"my notification message_"+index,"my notification ticker message_"+index,delay,true,true,requestCode);
			notificationRequestCodeCollection.Add(requestCode);
			
			Debug.Log("added scheduled notification with requestCode " + requestCode );
		}

	}
	
	public void CancelScheduledNotification(){
		//this is how we cancel or remove specific local notification 
		//assuming that in this scenario we want to cancel the previous local notifcation that we scheduled 
		//before it get fired,please try to avoid canceling or removing an already fired local notification because its useless
		
		if(notificationRequestCodeCollection.Count > 0){
			int prevRequestCode = notificationRequestCodeCollection.Count - 1;
			CancelSpecificNotification(prevRequestCode);
		}
	}
	
	
	//request code is the unique id of local notification use this to cancel or remove specific scheduled pending notification 
	private void CancelSpecificNotification(int requestCode){
		int len = notificationRequestCodeCollection.Count;
		Debug.Log("trying to cancel scheduled notification requestCode " + requestCode );
		
		for(int index=0;index<len;index++){
			int currentRequestCode = notificationRequestCodeCollection[index];
			if(currentRequestCode == requestCode){
				Debug.Log("found Scheduled notification with requestCode " + requestCode + " cancelling... ");
				localNotificationPlugin.CancelScheduledNotification(requestCode);
				break;
			}
		}
	}
	
	public void CancelAllNotification(){
		localNotificationPlugin.ClearAllScheduledNotification();
	}
}

 

Thank  you for visiting and reading our tutorial

Happy Coding 🙂

for comments or suggestions or questions or feedback just email us at gigadrillgames@gmail.com or you can comment below

15 thoughts on “Android Ultimate Plugin Tutorial part 9 – Local Notification

  • September 14, 2015 at 10:21 am
    Permalink

    hi, this notification is not working for me from the plugin downloaded from untiy

    Reply
    • September 16, 2015 at 3:23 pm
      Permalink

      Hi have you fixed the problem? and have you read the FAQ page, maybe your problem is Android Manifest permissions? do you have an Android Manifest file on the correct directory?

      Reply
    • September 19, 2015 at 8:31 am
      Permalink

      Hi Pons, actually its working maybe you just don’t know how it works and here’s what happen Local Notification will not fire when your application is currently running, to test that set a 3000 or 4000 interval on local nofication and then close your application after calling the local notification method. which is

      int delay = 4000;
      localNotificationPlugin.ScheduleNotification(“my notification title”,”my notification message”,”my notification ticker message”,delay,true,true);

      and i’m sure after that the local notification will going to work 🙂

      Reply
  • September 19, 2015 at 12:33 am
    Permalink

    i bought the asset,and everything is working fine,but why the local notification not working directly, i mean even when i change the time into 1ms from default 3000. it won’t appear if i only tap the notification,but the notification working when i tap the notification multiple times then tap the share text.

    please i need to know how this working.tq..

    Reply
    • September 19, 2015 at 8:33 am
      Permalink

      Hi John, here’s what happen Local Notification will not fire when your application is currently running, to test that set a 3000 or 4000 interval on local nofication and then close your application after calling the ScheduleNotification method. which is

      int delay = 4000;
      localNotificationPlugin.ScheduleNotification(“my notification title”,”my notification message”,”my notification ticker message”,delay,true,true);

      and i’m sure after that the local notification will going to work 🙂

      Reply
  • October 8, 2015 at 3:28 pm
    Permalink

    I am getting the vibration after the delay, but the notification does not appear in the status bar. Can you suggest anything that I may have missed?

    Reply
    • October 9, 2015 at 5:07 am
      Permalink

      Hi Ryan, What Plugin you are using Android Ultimate Plugin or Android Ultimate Plugin Lite? and about that having a vibration with notification that’s weird, what Android Device you are using? and what Android OS Version?

      Reply
  • October 12, 2015 at 10:26 pm
    Permalink

    Is it possible use this plugin lite without get task permission ? Thanks

    Reply
  • October 20, 2015 at 2:47 am
    Permalink

    Can I receive the local notification when the app is running?

    Reply
    • October 20, 2015 at 6:19 am
      Permalink

      Hi Question which one you are using Android Ultimate Plugin Lite (FREE) or Android Ultimate Plugin (Paid)? free one will not receive notification when app is running the paid one will still recieved

      Reply
  • January 12, 2016 at 10:24 am
    Permalink

    Hi, I bought Android Ultimate Plugin Lite.
    I have a question.
    App does not start when touch the notification.
    This is the same of AUP_1_4_4.apk.
    How can i fix it?

    best regards.

    environment: Android 4.1.2

    Reply
    • January 12, 2016 at 3:28 pm
      Permalink

      Hi Xevio, i just email you please reply to that 1st 🙂

      Reply
  • June 16, 2017 at 5:16 am
    Permalink

    Currently we can send local notification when we click on “send local notification how can we send it after installing app on set time interval with out clicking button when app is in background or foreground

    Reply
    • July 6, 2017 at 12:26 pm
      Permalink

      You need to code it , you can always set notifications using code anytime you want just make sure that your unity3d app is active when you do it not on minimize or inactive to avoid accessing null instance of our android plugin activity

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.