Local Notifications

To use Local Notifications you have Setup Local Notifications Settings in Inspector window.

Local Notifications settings contain:

  • Enable/Disable Show in foreground
  • Enable/Disable Notification Vibration
  • Setup Wake Lock Timeout
  • Setup Notification Large Icon
  • Setup Notification Small Icon
  • Setup Notification Sound

Here is the simple example how to schedule local notification, which will be fired in 10 seconds:

AndroidNotificationBuilder builder = new AndroidNotificationBuilder(
SA.Common.Util.IdFactory.NextId,"Local Notification Title",	"This is local notification", 10);

AndroidNotificationManager.Instance.ScheduleLocalNotification(builder);

If you don't set up Local Notification settings in Inspector, then default settings will be applied (default vibration, wake lock timeout, application icon for Local Notifications and defaut sound).

The NotificationId can be used to cancel the notification.

AndroidNotificationManager.Instance.CancelLocalNotification(LastNotificationId);

Or you can also cancel all scheduled local notifications using:

AndroidNotificationManager.Instance.CancelAllLocalNotifications();

To find out pending notification list, use:

List<LocalNotificationTemplate> scheduled = AndroidNotificationManager.Instance.LoadPendingNotifications ();

LoadPendingNotifications contains one default param bool includeAll = false. If you want to include all pending notifications you must to set this param as true:

List<LocalNotificationTemplate> scheduled = AndroidNotificationManager.Instance.LoadPendingNotifications (true);

Launch Notification

You can also find out if the app was launched with the click on local notification. And here is an example how:

//listening for load event
AndroidNotificationManager.Instance.OnNotificationIdLoaded += OnNotificationIdLoaded;
//loading app launch notification id
AndroidNotificationManager.Instance.LocadAppLaunchNotificationId();

private void OnNotificationIdLoaded (int notificationid){
	AN_PoupsProxy.showMessage ("Loaded", "App was laucnhed with notification id:" + notificationid);
}

If the returned notification is “-1” it means that the app wasn’t launched by clicking on local notification. But if it was will contain an id of notification with was used to launch the app.

Note: Restoring from background using click on local notification will not update

Toast Notification

You can always show simple toast notification as showed on the code bellow:

AndroidToast.ShowToastNotification ("Hello Toast", AndroidToast.LENGTH_LONG);

You can choose the duration:

public const int LENGTH_SHORT = 0; // 2 seconds 
public const int LENGTH_LONG  = 1; // 3.5 seconds

 

Advanced Local Notifications API

For more experienced developers there is another option for using Local Notifications.

Instead of passing notification title, notification message and notification time to ScheduleLocalNotification of AndroidNotificationManager, you can use AndroiNotificationBuilder instance to access more advanced API for creating Local Notifications.

AndroiNotificationBuilder class has next methods for Local Notification setup:

public void SetColor(NotificationColor color)
public void SetSoundName(string sound)
public void SetIconName(string icon)
public void SetVibration(bool vibration)
public void SetSilentNotification()
public void ShowIfAppIsForeground(bool show)
public void SetRepeating(bool repeat)
public void SetRepeatDelay(int delay)
public void SetLargeIcon(string icon)
public void SetBigPicture(Texture2D picture)
public void SetWakeLockTime(int wakeTime)

For using AndroidNotificationBuilder just create instance of this class and pass it to ScheduleLocalNotification method as shown below:

AndroidNotificationBuilder builder = new AndroidNotificationBuilder(
SA.Common.Util.IdFactory.NextId,"Title", "Notification message", 10);

AndroidNotificationManager.Instance.ScheduleLocalNotification(builder);

In this case such local notification settings as icon, sound and vibration, wake time lock will be used from Inspector settings.

To create local notification using advanced API just use additional methods of AndroiNotificationBuilder:

AndroidNotificationBuilder builder = new AndroidNotificationBuilder (ID, "Title", "Notification Message", 5);
builder.SetIconName ("notification_icon_name");
builder.SetSoundName ("notification_sound_name");
builder.SetVibration (true);

AndroidNotificationManager.Instance.ScheduleLocalNotification (builder);

Use SetSilentNotification method to create notification with NO sound and NO vibration.

AndroidNotificationBuilder builder = new AndroidNotificationBuilder (ID, "Title", "Notification Message", 5);
//Use notification with NO sound and with NO vibration
builder.SetSilentNotification ();

AndroidNotificationManager.Instance.ScheduleLocalNotification (builder);

Use SetRepeating and SetRepeatDelay APIs for scheduling the repeating notification. Such notifications will be fired up until for cancel them. So, please, make sure, that you saved the Id of the notification after scheduling it.

AndroidNotificationBuilder builder = new AndroidNotificationBuilder(SA.Common.Util.IdFactory.NextId,
                           "Local Notification Title",
						   "This is local notification",
		                   10); // The notification will be fired up in ten seconds after schedule
builder.SetBigPicture (bigPicture);
builder.SetRepeating (true);

System.DateTime now = System.DateTime.Now;
System.TimeSpan delay = now.AddDays (1.0f) - now;
builder.SetRepeatDelay (delay.TotalSeconds); // Set everyday repeating local notification

AndroidNotificationManager.Instance.ScheduleLocalNotification(builder);

Important:

All icons and sound for local notifications, that you will use in your Application, should be saved in Plugins/Android/AN_Res/res/drawable and Plugins/Android/AN_Res/res/raw accordingly.

For local notification icons:

For local notification sounds:

So, using advanced Local Notifications API, you can use in your application as many local notifications with different icon/sound/vibration options as you need.

Important: All icons/sounds resources should be named in lowercase.

Check if Notifications are ON?

The common questions: How to check programmatically if user turn off push notification in app settings?

It is asked in this video and the Project lead for the new notifications declares that you can't. Youtube video link.

Can I open app settings intent directly from the app to prompt user to turn it on? - Yes, this is possible with the Settings Page API.

Example Scene:

Plugins -> StansAssets -> Modules -> AndroidNative -> xExample -> Scenes -> Other -> Notifications