Implementing Android Notifications In Unity Game

Zoran Šaško
The Startup
Published in
7 min readFeb 1, 2021
Game Notification Preview¹

What are notifications and why they are important in applications or games?

A notification is a message that Android displays outside your app’s UI to provide the user with reminders, communication from other people, or other timely information from your app. Users can tap the notification to open your app or take an action directly from the notification.²

In a context of a game, notification can be used in many different contexts. For example, it can remind a user that he will receive some bonus if he returns to a gameplay, it can notify a user that some other player wants to play with him, it can be used to send some informations about discount for in-app purchase items in the game etc.

In the sample application that we are going to build in this article, we are going to explore how we can display inactivity notification — i.e. notification that will be scheduled and displayed 3 days after user didn’t started the game. If user opens the game before this 3 days period is done, ‘old’ inactivity notification will be deleted and new inactivity will be scheduled for the next 3 days period.

Notifications for inactivity are very useful for letting the users know that the game still exists on their phones and that they can resume gameplay. Otherwise they will stop playing the game or even worse, uninstall your game — and that’s what we are trying to prevent.

At the bottom of this article you can find a sample app that we are going to build in this article, so if you want to check the sample right now you are free to do so.

So, let’s dig in. 😃

In an empty Unity project, we’ll add the most important plugin for enabling notifications on Android/iOS platforms, named ‘Mobile Notifications’, via Unity’s Package Manager:

After we have installed the ‘Mobile Notifications’ in our project, we should see it in ‘Packages’ section of our project.

Next thing that’s important to import in project is ‘Mobile Notifications Wrapper’ which can be downloaded from here:

which is basically a wrapper around classes in ‘Mobile Notification’ package and it helps us to implement methods for scheduling notifications very easily.

So, download ‘NotificationsSample’ from the link above and extract it in ‘Assets’ folder of our project. Rename it from ‘NotificationsSamples-master’ to ‘NotificationsSample’, as it’s displayed on the following photo:

We have imported the required dependencies that will help us display mobile notifications on Android device, so we can start with more interesting part of this project. With designing the layout and writing some code, of course. 😃

The layout of our application will consist of single label that will display an information when the notification will be scheduled for display.

In the scene we are going to add a ‘Canvas’ game object (from ‘GameObject -> UI -> Canvas’ menu) and in ‘Canvas Scaler’ we are going to specify the property ‘UI ScaleMode’ to ‘Scale With Screen Size’, so the canvas is scaled appropriately on all screen sizes:

The next game object that we are going to add as a child of ‘Canvas’ is ‘Panel’ where we’ll set color to white with ‘alpha’ property set to 255:

As a child of ‘Panel’ game object, we are going to add a ‘Text’ game object, that will be used for displaying a message when next notification is going to be scheduled.

We can rename this ‘Text’ game object to ‘NotificationScheduledText’, so we’ll know for what this object is used. We are also going to change some of the properties of ‘NotificationScheduledText’ game object:

  • Position: X:0, Y:0, Z:0
  • Width: 230
  • Font size: 34
  • Alignment: center (both horizontal and vertical)
  • Vertical overflow: overflow

Before we start making the class that will invoke method for scheduling a notification, let us import a custom notification icons.

In the following link:

we are going to search for ‘videogame_asset’ (in ‘Clipart’ category) and download zip with notification icons in different resolutions.

We are going to import only two icons from the zip and firstly the icon from ‘drawable-xhdpi’ folder and this icon we are going to rename to ‘small_notification_icon.png’, and put it in newly created ‘Icons’ folder. Another icon that we are going to copy from extracted zip file is the one in ‘drawable-xxxhdpi’ folder, which we are going to rename to ‘big_notification-icon.png’ and also put in ‘Icons’ folder.

This is how the icons should be placed inside of ‘Icons’ folder.

The last step of icon customization is to change their type to ‘Sprite’, so click on first of the icons and in ‘Inspector’ panel change ‘Texture Type’ from ‘Default’ to ‘Sprite (2D and UI)’, enable checkbox ‘Read/Write Enabled’ and click ‘Apply’ button. Do the same for the second notification icon.

Now, we are going to assign these icons to be used in our app. We have to open ‘Mobile Notifications’ section of ‘Project Settings’ panel by opening this panel via ‘Edit -> Project Settings -> Mobile Notifications’ menu and put both notifications icons there as it’s displayed in the picture:

Notification icon identifiers ‘icon_0’ and ‘icon_1’ we are going to use later in coding section.

The next important step is to add a game object for handling of mobile notifications. So add an empty ‘GameObject’ to the scene, rename it to ‘Game Notification Manager’ and assign to it a ‘GameNotificationManager’ script in game object inspector panel.

From ‘GameNotificationManager’ script we’ll select ‘No Queue’ value of ‘Mode’ property:

More about queueing modes of ‘GameNotificationManager’ you can learn from here:

So far, we’ve made a lot of notification configuration setup. We’ve designed the very basic layout of our application and we are ready for writing some code that will actually schedule a notification.

Let us create empty game object named ‘AppNotificationManager’ and create-and-assign the same named script to it. Double click on ‘AppNotificationManager’ script in order to open it in C# editor and we’ll start writing some code.

At the top of ‘AppNotificationManager’ class we are going to add some GameObject references and constants (just above ‘Start’ method):

We are also going to need the following imports:

After we have added the code from above, we’ll connect references to ‘manager’ and ‘notificationDateText’. So, save all the changes in the ‘AppNotificationManager’ class and return to the scene.

Select ‘AppNotificationManager’ and keep it selected all the time. Drag ‘GameNotificationManager’ GameObject to ‘Manager’ property of ‘AppNotificationManager’ script and drag ‘NotificationScheduledText’ (located inside of ‘Canvas -> Panel’) to ‘Notification Scheduled Text’ property, like it’s displayed on the picture:

Very nice, let us resume writing code in ‘AppNotificationManager’ class and don’t forget to save the scene.

In ‘Start’ method we are going to invoke some function calls:

In order to display notifications in Android we need to initialize a channel which will be used for sending notifications.

More about notification channels you can read from here:

The method ‘ScheduleNotificationForUnactivity’, contains method calls for cancelling old notifications and scheduling new notification:

Method ‘ScheduleNotificationForUnactivity’ is invoking ‘SendNotification’ method with predefined notification attributes. It takes only one parameter ‘daysIncrement’ which specifies after how many days notification is going to be displayed.

Method that will display a scheduled notification timestamp is called ‘DisplayPendingNotification’ using ‘notificationScheduledText’ game object:

That’s it, we’ve written all the necessary code for scheduling a notification, so it’s time to build the app and see what we’ve done.

Don’t forget to add the current scene in ‘Build Settings’ and we can run the app. You can change current date in your phone and fast-forward it to 3 days in the future for the testing purposes.

If you want to learn more about how mobile notifications can be implemented and check all different options that mobile notifications can provide, check the link below:

Also, you can setup mobile notifications on iOS platform by following configuration steps from the link from above.

Source code of sample app:

[1]: “How to Block Notifications and Calls While Playing Games on Android Phones”, Itigic, https://itigic.com/block-notifications-calls-while-playing-games-android/.
Accessed 1 Feb. 2021.

[2]: “Notifications Overview”, Google Developers, https://developer.android.com/guide/topics/ui/notifiers/notifications. Accessed 31 Jan. 2021.

--

--