We’ve published a new package in the Preview repo that allows managing Mobile Notifications on Android & iOS directly from Unity.
It adds support for sending local notifications on Android. On iOS it is intended to replace the NotificationServices API (which has been available in Unity for sometime but uses a deprecated iOS API), and it implements the UserNotifications Framework.
For both Android and iOS key features are:
Schedule notifications with Time, Calendar or a Location(only iOS) triggers (repeatable or one time).
Manage notification channels on Android Oreo and above.
Cancel scheduled notifications and track their current status (on Android 6.0+).
Provide custom icons (on Android).
Customize presentation options (on iOS).
Implement custom C# callbacks that are triggered when local or push notifications are delivered (push notifications are currently only supported for iOS).
Group notifications into threads (on iOS 12+).
Change the App’s Icon badge number.
Supports Android 4.1+ (API 16) and iOS 10+.
You can download the Preview package by going to the Package Manager window inside the Unity Editor (the package is available for 2018.3 and above):
Click on the ‘Advanced’ button at the top of the window and click on “Show Preview Packages”
You should now see the ‘‘Mobile Notifications’ package in the list.
Alternatively you can just add this to your Packages/manifest.json file:
You can find the documentation which includes the API reference and some examples here.
We’ve also produced a Sample Project which implements a high level wrapper that allows sending notifications both to Android and iOS devices with the same API, you can find it on our GitHub page.
The package is still under development so if you have any feedback or suggestions please post them here. (Alternatively you can also use the Unity bug reporter to submit any bugs or issues you find)
@stopiccot_tds which 2018.2 version are you using, and did you get the error in Unity or Xcode? I’ve tried building that project with 2018.2.20f1 and it worked for me.
Yeah, you are right about java source files, I’m planning to switch to shipping them instead of the prebuilt library in the near future. Initially we planned to support older versions as well which would have made that a bit more complicated.
Hi everyone, we have found a that the Unity Mobile Notifications Package notification does not work when the Application is closed (fully closed application by a user. Any idea if this package support push notifications when the application is not running?
It does work, you just have to be careful with which data you pass when scheduling your notification.
The Android api uses UTC time, whilst the iOS api uses local time.
Notifications should still work both on iOS and Android when the app is closed, however currently scheduled notifications are not preserved when the device is restarted. But that’s a good point, I’ll try to make the Android API use local time as well.
@Sylmerria we don’t have any specific plans to support push notifications on Android (but it’s something we’re considering to implement at some point in the future).
There are 6 warning after I downloaded the package. What are these talking about and do I have to fix something?
/Users/Library/Unity/cache/packages/packages.unity.com/com.unity.mobile.notifications@1.0.0-preview.8/Runtime/Android/AndroidNotificationCenter.cs(482,20): warning CS0414: The private field `Unity.Notifications.Android.AndroidNotificationCenter.AndroidSDK’ is assigned but its value is never used
/Users/Library/Unity/cache/packages/packages.unity.com/com.unity.mobile.notifications@1.0.0-preview.8/Editor/UnityNotificationEditorManager.cs(405,25): warning CS0219: The variable `textXxhdpi’ is assigned but its value is never used
/Users/Library/Unity/cache/packages/packages.unity.com/com.unity.mobile.notifications@1.0.0-preview.8/Editor/UnityNotificationEditorManager.cs(480,17): warning CS0219: The variable `textureFormat’ is assigned but its value is never used
/Users/Library/Unity/cache/packages/packages.unity.com/com.unity.mobile.notifications@1.0.0-preview.8/Editor/UnityNotificationsEditorManagerEditor.cs(36,22): warning CS0414: The private field `Unity.Notifications.UnityNotificationsEditorManagerEditor.identifierLabelText’ is assigned but its value is never used
/Users/Library/Unity/cache/packages/packages.unity.com/com.unity.mobile.notifications@1.0.0-preview.8/Editor/UnityNotificationsEditorManagerEditor.cs(37,22): warning CS0414: The private field `Unity.Notifications.UnityNotificationsEditorManagerEditor.typeLabelText’ is assigned but its value is never used
/Users/boat/Library/Unity/cache/packages/packages.unity.com/com.unity.mobile.notifications@1.0.0-preview.8/Runtime/iOS/iOSNotificationCenter.cs(663,60): warning CS0067: The event `Unity.Notifications.iOS.AuthorizationRequest.OnAuthRequest’ is never used
@Thanitsak You can’t fix those warnings yourself as the files can’t be accessed by us. If you use a warning as error type system you just have to disable it and clear the warnings, I ran into the same issue.
PauliusP: “… however currently scheduled notifications are not preserved when the device is restarted.”
If anyone is wondering ‘well how do I fix this then’, you will just have to save the identifiers of the scheduled notifications in playerprefs and request them the next session, allowing you to cancel them etc.
For anyone that wants to use this package seriously I suggest you write a generic system that uses wrapper classes that wrap around the android and ios functionality, because how it’s set up right now really isn’t handy to use and looks like it has been written by three different people. Good luck.
Sorry for not including the test device Android device - it is Huawei android 6.0. Everything works, but when application if fully closed - no notifications are coming (We are using UTC only time both for IOS and Android).We have tested it on other android devices and they have android 7 or 8 (notification are coming even when closed), same for IOS 10,11,12.
The warning messages are fixed in 1.0.0-preview9 which should be available in the package manager later today.
@ we already have a cross-platform wrapper available here. But yes I agree that the Android & iOS parts are quite different and to some extent this is intentional: they are supposed to mostly map directly to native APIs. Certain Android specific concepts don’t really transfer that well to iOS and this is also partially true the other way around. In the future the wrapper I’ve linked (or something similar to it) will be included in the package itself.
@studentutu that’s strange, it might be something specific to Android 6.0, could you please submit a bug report with the Unity bug reporter? Please list the devices you’ve tested on and their OS version and add the script you’ve used to schedule the notifications if possible.
Hi.
Is there a way detect which notification caused the app to launch?
I would like to pass some custom data to a notification during scheduling that I can later retrieve when user taps that notification. This way, I can redirect user to related screen automatically depending on which notification he tapped.
Thanks
As far as I am aware it is not possible with the current functionalities, what you need for that is deeplinking, you could search around for some other plugins that do support that.
When I tested notifications were not working in android Pie and Oreo devices. I tried setting fireTime to System.DateTime.Now.AddSeconds(10) and System.DateTime.UtcNow.AddSeconds(10).
The code I used is given below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Notifications.Android;
public class LocalNotificationManager : MonoBehaviour
{
private AndroidNotificationChannel androidNotificationChannel;
private const string channelId = "0";
// Use this for initialization
void Start ()
{
DontDestroyOnLoad(gameObject);
}
private void CreateNotificationChannel()
{
androidNotificationChannel = new AndroidNotificationChannel();
androidNotificationChannel.Id = channelId;
androidNotificationChannel.Name = "Default Channel";
androidNotificationChannel.Importance = Importance.High;
androidNotificationChannel.Description = "Generic notifications";
AndroidNotificationCenter.RegisterNotificationChannel(androidNotificationChannel);
}
private void OnApplicationPause(bool pause)
{
if(pause)
{
SendNotification();
}
else
{
AndroidNotificationCenter.CancelAllNotifications();
}
}
private void SendNotification()
{
AndroidNotification notification = new AndroidNotification();
notification.Title = "Test";
notification.Text = "Test";
notification.FireTime = System.DateTime.Now.AddSeconds(10);
int id = AndroidNotificationCenter.SendNotification(notification, channelId);
Debug.Log("Notification status = " + AndroidNotificationCenter.CheckScheduledNotificationStatus(id));
}
}
EDIT: I had forgotten to create notification channel. Now it is working.
How do I know the ID of the notification when I clicked?
Can I open a scene when somone click the notification?
The notifications works fine on Android, but when the phone locks and unlocks, the notification doesn’t remove when clicked
void AndroidNotification()
{
var c = new AndroidNotificationChannel()
{
Id = "channel_id",
Name = "Default Channel",
Importance = Importance.High,
Description = "Generic notifications",
};
AndroidNotificationCenter.RegisterNotificationChannel(c);
var notification = new AndroidNotification();
notification.Title = "Give me some food!";
notification.Text = "test test test test test test test ";
notification.FireTime = System.DateTime.Now.AddSeconds(30);
notification.Color = Color.red;
notification.Style = NotificationStyle.BigTextStyle;
int identifier = AndroidNotificationCenter.SendNotification(notification, "channel_id");
Debug.Log("Identifier: " + identifier.ToString());
}
Hey,
Nice and useful package that saves getting something off the asset store. I am getting the same issue as @stopiccot_tds . I am using 2018.2.14f1. I was also wondering if Rich notifications are planned for a future release with this as they would be nice to have.