Free Android local notifications plugin open source

Check my repository GitHub - Agasper/unity-android-notifications: Unity3D Plugin for Android local notifications with example project
License: MIT
Features:

  • Set delayed notification
  • Set delayed repeatable notification
  • Supports custom icon and large icon
  • Fully supports Android 5.0
5 Likes

Cool, adding it now, thanks! I was looking for a simple solution, without all the junk I don’t need.

Quick Q: What would the delay be for say once per week? I’m only guessing it’s in seconds…

You have to put delay in seconds, in LocalNotification.cs it converts to milliseconds and come to plugin.
1 week = 60 * 60 * 24 * 7 = 604800

1 Like

Cool plugin! Is it easy to add a firedate in the Android Java code? So let’s say I want daily repeating notifications on:
8:00
13:00
19:00

You can get delta seconds between now and firedate:

DateTime dt = new DateTime(2015, 4, 1, 14, 30, 0, 0);
long deltaSeconds = (dt - DateTime.Now).TotalSeconds;

Thanks for the reply, I’ve tried to add this but I can’t test if it works because I get an error that the Java class can’t be found. I’m guessing there is an error in my AndroidManifest.xml
Can you take a look at it?

<?xml version="1.0" encoding="utf-8"?>
<manifest
        xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.devfo.andutils"
        android:versionName="1.2"
        android:versionCode="2">
    
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />

    <supports-screens
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:xlargeScreens="true"
        android:anyDensity="true" />
    
    <application
        android:icon="@drawable/app_icon"
        android:label="@string/app_name"
        android:debuggable="true">
    
        <!-- NOTIFY java -->
        <receiver android:name="net.agasper.unitynotification.UnityNotificationManager"></receiver>
        <!-- end -->

        <activity
            android:name="com.devfo.andutils.DevfoUnityPlayerActivity"
            android:label="@string/app_name"
            android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
            android:screenOrientation="portrait">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                    <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
                </intent-filter>
                <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
                <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
        </activity>

        <activity android:name="com.unity3d.player.VideoPlayer" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:screenOrientation="portrait">
        </activity>
    </application>

</manifest>

And my code for adding the notification:

private static string fullClassName = "net.agasper.unitynotification.UnityNotificationManager";
private static string unityClass = "com.unity3d.player.UnityPlayerNativeActivity";

DateTime temp = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute + 1, DateTime.Now.Second);

CreateTempNotification("Test", "Message", temp, 4);

private void CreateTempNotification(string title, string message, System.DateTime fireDate, int KeyValue)
    {
        Debug.Log("CREATING TEMP NOTIFICATION AT DATE: " + fireDate.ToString());

        #if UNITY_ANDROID && !UNITY_EDITOR
        AndroidJavaClass pluginClass = new AndroidJavaClass(fullClassName);
        if(pluginClass != null)
        {
            bool sound = true;
            bool vibrate = true;
            bool lights = true;

            long notif01 = (long)(fireDate - DateTime.Now).TotalSeconds;

            Debug.Log ("Date1: " + notif01);

            pluginClass.CallStatic("SetRepeatingNotification", KeyValue, unityClass, notif01, title, message, message, 60*60*24 * 1000, sound ? 1 : 0, vibrate ? 1 : 0, lights ? 1 : 0);
        }
        #endif
 
        Debug.Log ("SEND TEMP NOTIFICATION");
    }

Try to change unityClass to “com.devfo.andutils.DevfoUnityPlayerActivity”, seems that you have plugins conflict

Thanks again for the reply! I’m still getting this error when running my game and creating the notification:

I’ve changed the unityClass to “com.devfo.andutils.DevfoUnityPlayerActivity”

Try to rollback and remove other plugins

So I removed all the plugins and reimported the files from your demo project. It’s working now! Only problem i’m having, is when sending a notification (not repeating) I see the notification and icon in the notification bar on my Android Phone (vibration and light is also working). But when scheduling a repeating notification I get the vibration and the light but no icon, title and message is showing in the bar. Is this a bug?

Plugin updated. Bugs fixed and added better compatibility with Android Lollipop

1 Like

Great it’s working perfect now! One thing I would advise you to do in your code. In the SendRepeatingNotification (Unity3D script) you use long timeout, wouldn’t it be better to change that to repetition?

Hi Nihilitik

I’ve implemented the Android notifications, and it’s working fine. But there is a small problem that I hope you know how to get fixed. When I set the notifications, there are always 2 firing immediately. Thereafter it works like it’s suppose to work. Here’s my code:

public void CreateNotification()
{
DateTime date01 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 08, 00, 00);
            DateTime date02 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 13, 00, 00);
            DateTime date03 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 19, 00, 00);

SendRepeatingNotification(1, date01, Language.Get("Notification_Header01"), Language.Get("Notification_Message01"), new Color32(18, 115, 196, 255));
            SendRepeatingNotification(2, date02, Language.Get("Notification_Header01"), Language.Get("Notification_Message01"), new Color32(18, 115, 196, 255));
            SendRepeatingNotification(3, date03, Language.Get("Notification_Header01"), Language.Get("Notification_Message01"), new Color32(18, 115, 196, 255));
}
public static void SendRepeatingNotification(int id, DateTime fireDate, string title, string message, Color32 bgColor, bool sound = true, bool vibrate = true, bool lights = true, string bigIcon = "")
    {
        long delay =(long) (fireDate - DateTime.Now).TotalSeconds;
        long rep = 60 * 60 * 24;
        #if UNITY_ANDROID && !UNITY_EDITOR
        AndroidJavaClass pluginClass = new AndroidJavaClass(fullClassName);
        if (pluginClass != null)
        {
            pluginClass.CallStatic("SetRepeatingNotification", id, unityClass, delay * 1000L, title, message, message, rep * 1000, sound ? 1 : 0, vibrate ? 1 : 0, lights ? 1 : 0, bigIcon, "notify_icon_small", bgColor.r * 65536 + bgColor.g * 256 + bgColor.b);
        }
        #endif
    }

fireDate should be always greater than DateTime.Now. You are creating dateXX wrong

1 Like

I feel silly thx man! Fixed it and now it’s working perfectly :slight_smile:

Hi Nihilitik
your code is awesome, it’s working when i build alone in android.

but when i build with Vuforia plugin and i change the AndroidManifest.xml
the script notification not work
can you help me ??

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.qualcomm.QCARUnityPlayer" android:versionCode="1" android:versionName="1.0">

  <!-- NOTIFY java -->
  <receiver android:name="net.agasper.unitynotification.UnityNotificationManager"></receiver>
  <!-- end -->
   
  <uses-sdk android:minSdkVersion="8" />
  <uses-feature android:name="android.hardware.camera" />
  <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:anyDensity="true" />
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.CAMERA" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.VIBRATE" />
  <application android:icon="@drawable/app_icon" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:debuggable="false">
    <activity android:name="com.qualcomm.QCARUnityPlayer.QCARPlayerNativeActivity" android:label="@string/app_name" android:screenOrientation="sensor" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
      <meta-data android:name="android.app.lib_name" android:value="unity" />
      <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <activity android:name="com.unity3d.player.VideoPlayer" android:label="@string/app_name" android:screenOrientation="sensor" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
    </activity>
    <activity android:name="com.devfo.andutils.DevfoUnityPlayerActivity" android:label="@string/app_name" android:screenOrientation="sensorLandscape">
    </activity>
  </application>
 
</manifest>
<!-- android:installLocation="preferExternal" -->

If you changed default Unity activity to Vuforia in manifest, you have to change it in LocalNotification.cs:9
to com.qualcomm.QCARUnityPlayer.QCARPlayerNativeActivity

I’m testing your plugin and it work fine with your unity test project, but when I try to integrate it to my game doesn’t work. I think it maybe an issue with other plugins I have in the game like Google Play Games Services, AdColony, Everyplay and Google Analytics. Each plugins of these brings their own manifest that are merged by unity at compilation.

My question is: do you know any common issues that can appear when these plugins are put all together?

Also I noted that your test project is targeting to Android 4.1, this is the minor version that I need to target when use this plugin?

I also need to open the game when the user touch the notification, it would great for everyone if you can add it.

Thanks in advance, good job with this plugin.

Please post your manifest here