Android Mobile Notifications With Unity
In this tutorial, you’ll learn about Unity’s Mobile Notifications and how to create and schedule your very own notifications for a Unity app running on an Android device. By JJ Richards.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Android Mobile Notifications With Unity
25 mins
- Getting Started
- Setting the Scene
- Using Notifications to Improve Retention
- Understanding Notifications
- Prepping Assets
- Initializing the System
- Sending a Manual Notification
- Implementing the ShortNote
- Implementing the LongNote
- Setting up the UI Buttons
- Scheduling a Group of Notifications
- Retention Reminders
- Scheduled Events
- Starting Fresh
- Overusing Notifications
- Where to Go From Here?
Setting up the UI Buttons
In the Hierarchy, select the Button component of the ShortNote GameObject, create a new OnClick event by clicking the + button at the bottom-right and drag the NotificationManager GameObject into the newly-created OnClick slot. In the drop-down, select NotificationManager.ShortNote.
Repeat that step for the LongNote GameObject, assigning NotificationManager.LongNote.
Save the scene and the project. You’re ready to deploy a build to your Android device.
Test by clicking your new buttons and waiting for the notifications to appear. Try it with the app open and closed. The notifications should still appear as scheduled.
Congratulations, you have mastered the basics of notifications. Next, it’s time for something a little more advanced.
Scheduling a Group of Notifications
Retention reminders and scheduled events are powerful forms of notifications, which differ slightly from the basic time-delay format.
Retention Reminders
You should be able to cancel retention reminders if the player returns before they occur. For example, reminding the player to return 3-days, 7-days, and 30-days after their last session is a common approach.
If the player never returns, they’ll get three notifications on the 3rd, 7th and 30th day. But imagine they come back on the 5th day. The previously-scheduled reminders for the 7th and 30th day need to be canceled. And all three reminder days need to be reset from the new current play date.
To create retention reminders, add the following code to the Notification Manager:
private string retentionIconName = "icon_0";
private string retentionLargeIconName = "icon_1";
//Schedule notification relative to current game session
//Change this from seconds to days after testing
private void RetentionReminderTest(int seconds)
{
string title = "We miss you!";
string body = string.Concat("You've been away for ",seconds," seconds");
DateTime deliverTime = DateTime.UtcNow.AddSeconds(seconds);
string channel = ReminderChannelId;
SendNotification(title, body, deliverTime, channelId: channel,
smallIcon: retentionIconName, largeIcon: retentionLargeIconName);
}
If you think this looks a lot like both ShortNote
and LongNote
, you’re right! It’s simply a generic form that takes the amount of time to wait as a parameter. So you can call it with any amount of time that you need.
Scheduled Events
Scheduled events need to happen at a specific date and time, regardless of when the player last used the app. Maybe there’s a recurring event on the 15th of every month or a one-time event scheduled for a specific holiday, like Mother’s Day. DateTime.UtcNow.AddSeconds
doesn’t help in this scenario, so you’ll need to create another specific function to schedule these notifications by adding the following code to the Notification Manager:
private string eventIconName = "alarm";
private string eventLargeIconName = "icon_1";
//Schedule notification to a specific DateTime
//Change this from minutes to a specific DateTime or daysOfTheMonth
private void EventAlarmTest(int minutesOnTheHour)
{
string title = "Event is Starting!";
string body = string.Concat("It's ", minutesOnTheHour, " minutes after the hour");
DateTime deliverTime = GetNextEvent(minutesOnTheHour);
string channel = ChannelId;
SendNotification(title, body, deliverTime, channelId: channel,
smallIcon: eventIconName, largeIcon: eventLargeIconName);
}
private DateTime GetNextEvent(int minutesOnTheHour)
{
DateTime temp = DateTime.Now;
temp = new DateTime(temp.Year, temp.Month, temp.Day,
temp.Hour, minutesOnTheHour, 0);
return temp;
}
This one has the similar structure, but uses a different icon from the Mobile Notification Settings. It also has a helper for the helper: GetNextEvent
, which helps translate an amount of time that you want to wait into the specific DateTime
format that you need to send to SendNotification
.
You can design similar functions to help you achieve any type of notification scheduling you need.
Starting Fresh
There are a couple of last pieces to put the entire puzzle together.
At the bottom of the Notification Manager, add a helper function that calls the retention reminder and event scheduling functions that you want:
void ScheduleCurrentNotifications()
{
//1
RetentionReminderTest(30);
//2
RetentionReminderTest(70);
//3
RetentionReminderTest(100);
//4
EventAlarmTest(DateTime.Now.Minute + 2);
//5
EventAlarmTest(DateTime.Now.Minute + 3);
}
- Schedule a retention reminder for 30 seconds
- Schedule another retention reminder for 70 seconds
- Schedule a third retention reminder for 100 seconds
- Schedule a specific event for 2 minutes in the future
- Schedule another specific event for 3 minutes in the future
There are many scenarios where scheduled notifications need to be replaced or updated. Add a step to the system flow that removes all previously-scheduled notifications. Every time the player launches the app, previously-scheduled notifications should be discarded and replaced with a fresh set of notifications scheduled from the current playdate.
To implement this feature, add this line to the Start section of Notification Manager:
manager.Platform.CancelAllScheduledNotifications();
That was easy.
Finally, call the ScheduleCurrentNotifications function at the bottom of Start of the Notification Manager. The full Start function should look like this:
void Start()
{
var c1 = new GameNotificationChannel(ChannelId, "Game Alerts", "Game notifications");
var c2 = new GameNotificationChannel(NewsChannelId, "News", "News and Events");
var c3 = new GameNotificationChannel(ReminderChannelId,
"Reminders", "Reminder notifications");
manager.Initialize(c1, c2, c3);
manager.Platform.CancelAllScheduledNotifications();
ScheduleCurrentNotifications();
}
Save the NotificationManager script – it’s now complete.
Return to the editor to save the project and deploy it to your Android device. The UI buttons should work the same as before, now the additional notifications fire on the device after you load it. Close and open the app to observe which notifications reset over time.
Congratulations! You’ve mastered advanced notifications.
Overusing Notifications
Armed with your new notification skills, you wield great power that comes with great responsibility, as the cliché goes. While notifications are great when used well, it only takes one annoying notification to get the user to turn off ALL notifications coming from your app.
Even important notifications, like reminders for your mother’s birthday, that arrive too often or in the middle of the night can frustrate the user enough to turn off ALL future reminders. So be conservative when selecting and scheduling which notifications to send.
Where to Go From Here?
Great job on completing this tutorial! You can download the completed project files by clicking the Download Materials button at the top or bottom of the tutorial.
Feel free to tweak the project to your needs and turn it into something that gets you noticed. :]
The Unity Mobile Notifications Package will continue to evolve, so follow the latest updates here: Unity Mobile Notifications Package.
If you’re interested in notifications on iOS, watch our Push Notifications Video Course or read our book, Push Notifications by Tutorials.
I hope you enjoyed this tutorial. If you have any questions or comments, please join the forum discussion below!