Reader small image

You're reading from  Unity 2022 Mobile Game Development - Third Edition

Product typeBook
Published inJun 2023
Reading LevelIntermediate
PublisherPackt
ISBN-139781804613726
Edition3rd Edition
Languages
Tools
Right arrow
Author (1)
John P. Doran
John P. Doran
author image
John P. Doran

John P. Doran is a passionate and seasoned Technical Game Designer, Software Engineer, and Author who is based in Incheon, South Korea. His passion for game development began at an early age. He later graduated from DigiPen Institute of Technology with a Bachelor of Science in Game Design. For over a decade, John has gained extensive hands-on expertise in game development working in various roles ranging from game designer to lead UI programmer working in teams consisting of just himself to over 70 people in student, mod, and professional game projects including working at LucasArts on Star Wars: 1313. Additionally, John has worked in game development education teaching in Singapore, South Korea, and the United States. To date, he has authored over 10 books pertaining to game development. John is currently a Technical Game Design Instructor at George Mason University Korea. Prior to his present ventures, he was an award-winning videographer.
Read more about John P. Doran

Right arrow

Keeping Players Involved with Notifications

One of the best ways to keep users coming back to your game is through the use of push notifications. This allows you to stay in contact with your users even when they’re not using your game. Used wisely, this can keep users playing your game for a long period of time. Using notifications too often or poorly will cause users to mute your app’s notifications, which is not an ideal situation.

In this chapter, we will explore how to create notifications for both Android and iOS devices. We will then learn how to schedule notifications to keep players returning to the game later on, as well as ways that we can customize them.

The chapter is split into a number of topics. It contains a simple, step-by-step process from beginning to end. Here is the outline of our tasks:

  • Setting up notifications
  • Scheduling notifications ahead of time
  • Customizing notification presentation
  • Canceling notifications

Technical requirements

This book utilizes Unity 2022.1.0b16 and Unity Hub 3.3.1, but the steps should work with minimal changes in future versions of the editor. If you would like to download the exact version used in this book, and there is a new version out, you can visit Unity’s download archive at https://unity3d.com/get-unity/download/archive. You can also find the system requirements for Unity at https://docs.unity3d.com/2022.1/Documentation/Manual/system-requirements.html in the Unity Editor system requirements section. To deploy your project, you will need an Android or iOS device.

You can find the code files present in this chapter on GitHub at https://github.com/PacktPublishing/Unity-2022-Mobile-Game-Development-3rd-Edition/tree/main/Chapter09.

Setting up notifications

Before we can start adding notifications to our project, we will need to add a special preview package that Unity makes available. Follow the steps given here:

  1. From the Unity Editor, go to Window | Package Manager.
  2. From there, if the top-left section does not say Packages: Unity Registry, click on the In Project drop-down menu from the toolbar of the Packages menu and select Unity Registry.
  3. Scroll down the available options until you reach Mobile Notifications and select it. Once there, click on the arrow to the side of it and select See All Versions and then select the latest version (in my case, it was Version 2.0.2). From there, click the Install button and you’ll see the following screenshot:
Figure 9.1: Installing the Mobile Notifications package

Figure 9.1: Installing the Mobile Notifications package

Note

It’s important to note that this package requires your game to use Android 5.1 (API level 22) and iOS 10 or above in order to function properly...

Scheduling notifications ahead of time

One of the most common forms of creating a notification is asking players to come back and play the game at a later time. This encourages users to continue playing our game and to come back multiple times. We can do this by setting a delivery time in the future using the following steps:

  1. Open up the NotificationsController script and add the following function to it:
    public void ShowNotification(string title, string body,
    DateTime deliveryTime)
    {
        IGameNotification notification =
            notificationsManager.CreateNotification();
        if (notification != null)
        {
            notification.Title = title;
            notification.Body = body;
            notification.DeliveryTime = deliveryTime;
        notificationsManager...

Customizing notifications

Unity includes some default visuals to be used with notifications, but generally, replacing the content with our own will help our game stand out and be more visually appealing to players. In order to have custom icons for Android notifications, you are required to have a small icon with at least 48 x 48 pixels, and have only white pixels with a transparent backdrop. The large icon must be at least 192 x 192 and can have whatever colors we’d like. You can create images of your own, or use the images named Hi-ResIcon.png and Small-ResIcon.png provided in the example code for this book in the Chapter 08\Assets\Sprites folder of the GitHub repository. Follow the steps given here for customization:

  1. From the Project window, select the images you are planning to use for the small and large icons.
  2. With the images selected, go to the Inspector window and check the Alpha Is Transparency property.
  3. Finally, open up the Advanced options and check...

Canceling notifications

There are several reasons why we may want to cancel notifications. This could be due to the fact that, during play, the player has made a decision that has caused something to no longer be relevant.

For this example, let’s create a sample notification that we will cancel before it has a chance to happen. But for us to cancel a notification, we must have a way of knowing which notification is which. Thankfully, the Notifications Manager has a property called an Id that each notification has. We can set the notification ourselves by hand or Unity will generate it for us. In our case, we will use the automatically generated one:

  1. Open up the NotificationsController script and go to the ShowNotification function. Update it to the following:
    public int? ShowNotification(string title, string body, DateTime deliveryTime)
    {
        IGameNotification notification =
        notificationsManager.CreateNotification();
     ...

Summary

At this point, we have seen how we can make use of Unity’s Mobile Notifications package to create notifications for our players. We’ve learned how to schedule them to take place in the future as well as how to customize these notifications to have our own distinct visual style!

We now have everything in place for players to play and come back to our game, but we are only relying on what we created. In addition to that, we may want to see what our players are doing while playing our games. Then, we can use that information to improve and/or tweak our game.

In the next chapter, we will take a look at how we can do this using tools from Unity Analytics.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Unity 2022 Mobile Game Development - Third Edition
Published in: Jun 2023Publisher: PacktISBN-13: 9781804613726
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
John P. Doran

John P. Doran is a passionate and seasoned Technical Game Designer, Software Engineer, and Author who is based in Incheon, South Korea. His passion for game development began at an early age. He later graduated from DigiPen Institute of Technology with a Bachelor of Science in Game Design. For over a decade, John has gained extensive hands-on expertise in game development working in various roles ranging from game designer to lead UI programmer working in teams consisting of just himself to over 70 people in student, mod, and professional game projects including working at LucasArts on Star Wars: 1313. Additionally, John has worked in game development education teaching in Singapore, South Korea, and the United States. To date, he has authored over 10 books pertaining to game development. John is currently a Technical Game Design Instructor at George Mason University Korea. Prior to his present ventures, he was an award-winning videographer.
Read more about John P. Doran