Android Mobile Notifications With Unity
- 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?
Ding! Time to wake up.
Ding! You have an appointment in 30 minutes.
Ding! Tomorrow is your mother’s birthday.
Notifications are a common and valuable part of modern life, so it’s natural to expect your favorite mobile apps and games to use them too. 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. Notifications will take your apps to the next level, helping you to retain user attention and also to keep your users informed.
Download the project by clicking the Download Materials button at the top or bottom of the tutorial.
Open the starter project and look at the folder structure in the Project window.
In Assets ► RW, you’ll find everything you need for the project:
- Scenes: Contains the NotificationTutorial scene.
- Scripts: Empty folder for the script you’ll create.
- Textures: Contains sample art for you to use.
- Prefabs: A sample interface for you to use.
Take a quick look at the NotificationTutorial scene in the Scenes folder. You’ll notice that it’s just an empty scene with nothing much going on. It represents a typical Unity app with no Notification capabilities. In the steps to follow, you’ll be adding some basic UI elements, you’ll also be adding the required components and scripts to enable this Unity app to send Notifications.
Now you’re ready to start getting noticed! :]
Setting the Scene
Since this is an Android tutorial, make sure you select Android as the platform in your Build Settings. Open the Package Manager and you’ll see that TextMeshPro and Mobile Notifications are already imported.
Open the NotificationTutorial scene inside the Scenes folder. You’ll find a very basic scene with a camera, a Screen Overlay Canvas and an EventSystem. Set your Game view to Portrait to match the default orientation configured in Project Settings ► Player.
Start by creating a title for the main page. Select Canvas in the Hierarchy, add a UI ► Text – TextMeshPro GameObject and call it Title. Give the text field a catchy name: Notation Tutorial.
Create an empty GameObject under Canvas to hold some buttons. Call it ButtonGroup and add a Horizontal Layout Group component to adjust the layout as you like. You can also adjust the RectTransform to suit your preferences.
Next, create two buttons by selecting ButtonGroup in the Hierarchy and then right-clicking to select UI ► Button-TextMeshPro.
You’ll use the first button to send a single notification after a short delay, so name it ShortNote. Set the Text to Notify in 10 seconds.
You’ll use the second button to schedule a single notification after a long delay, so name it LongNote. Set the Text to Notify in one minute.
If you don’t want to set this up yourself, there’s a prefab ready to import in RW ► Prefabs.
The UI is now complete. Before implementing specific notifications, take a moment to consider why notifications are important for your app.
Using Notifications to Improve Retention
One of the primary benefits of notifications is the positive impact they have on player retention. Notifications help bring players back to a game they haven’t played in a while. These player lapses are often addressed on a 3-day, 7-day or 30-day cycle from the player’s last active session.
Notifications help players start a session at the right time via timers and alerts related to gameplay events. For example, you might notify players when the smithy has finished their brand new sword or their castle is under attack.
You can also use notifications to improve retention by alerting players about scheduled events and tournaments, whether created by the developer or other players.
All these examples are compelling reasons for a player to come back to a game, and notifications are a great way to remind players about those reasons.
The notification itself has five parts that you’ll need to create and provide:
- Title: A short, bolded text.
- Description: A longer, non-bolded text.
- Small icon: A small, pure white icon that appears throughout the device UI.
- Large icon: A large, colored icon that only appears when space in the UI is available.
- Delivery date: The date and time when the app should notify the user.
As the app developer, you don’t control the device UI or how the user interacts with the notification – that’s the job of the device OS. Your job is to supply all five parts of the notification so the device OS can properly send the message according to a variety of user scenarios.
Now that you have an idea of the structure behind the notifications, it’s time to get your assets ready to go.
To have the best effect, your notifications shouldn’t only be timely, they should also be attractive. For that, you’ll need some artwork.
Unity’s Mobile Notifications Package provides an easy way to manage the art assets you need to create for notifications. This tutorial uses Mobile Notifications Package preview.5 – 1.0.4 from Unity’s Package Manager. You can find the Mobile Notifications Settings under Project Settings.
Your first step is to set up three icons:
- For Identifier icon-0 with Type Small Icon, select the icon_timer asset.
- Next, for Identifier icon_1, which has Type Large Icon, select the RW_Icon_macOS_256 asset.
- Finally, for Identifier alarm, with Type Small Icon, select the icon_notification_bell asset.
These identifiers need to match the script that you’ll create next. The settings dialog should look like this:
The scene also needs a way to process notifications.
To set these up, create an empty GameObject called NotificationManager. The scene hierarchy should now look like this:
Select that GameObject and add a Game Notifications Manager component, which comes with the Mobile Notifications Package.
Inside Scripts, create a new C# script named NotificationManager. Attach that script to the NotificationManager GameObject as well.
Now that all the pieces are in place, it is time to connect them.