In this tutorial, I will show you how to implement the mobile notifications on an android phone with Unity 2020

Speedy square is now available on Google Play

With this tutorial, you will be able to show notifications each sundays and wednesday. There is also one example for a simple notification who will show the reminder the next day

1. Package installation

First, you need to import the official page named “Mobile notifications” from Unity.

Go to Window > Package Manager > Mobile Notifications then click on the Import button

If you can’t see this package, be sure to select “Unity Registry” in Packages

2. Notifications Manager

Create a new game object called “NotificationsManager” in the hierarchy. Add a new script called “NotificationsManager.cs”

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Unity.Notifications.Android;

public class NotificationManager : MonoBehaviour {
    
    // Start is called before the first frame update
    void Start() {
        // Cancel all previous notifications
        AndroidNotificationCenter.CancelAllNotifications();

        Reminder ('You miss us!', 'Hey player. It's time to play again.');
    }

    /**
     * Send Reminder 
     * @param title
     * @param text
     */
    public void Reminder(string title, string text) {
        // Init channel for the notification
        var channel =
            new AndroidNotificationChannel()
            {
                Id = "reminder",
                Name = "SpeedySquare",
                Importance = Importance.High,
                Description = "a description for your channel"
            };

        AndroidNotificationCenter.RegisterNotificationChannel (channel);

        // Find next sunday
        int start = (int) System.DateTime.Now.DayOfWeek;
        int target = 0; // 0 => sunday, 1 => monday, etc...
        if (target <= start) target += 7;

        // Prepare the notification for Sunday AND Wednesday (in Timespan => 72 = 72 hours so... 3 days)
        var notification = new AndroidNotification(title, text, System.DateTime.Now.AddDays((target - start)), new System.TimeSpan(72,0,0));  
        
        // Simple notifications reminder (will be shown the next day)
        var notification = new AndroidNotification(title, text, System.DateTime.Now.AddDays(1));

        // Send the notifications
        AndroidNotificationCenter.SendNotification(notification, "reminder");
    }
}

This script will be called each time the player goes to the current scene (for example the main menu).

Now, if you want to test your notifications, you can change the date in the settings of your smartphone. Simple right?

Known issues (in this case)

I have encoured a bug. When I have builded the project as an aab file, the notification didn’t work anymore, only when I’ve checked “Development Build”.

The solution: Under Project Settings then Player and finally Publishing Settings, uncheck the box Release and Debug under the section Minify

Leave a Reply

Your email address will not be published. Required fields are marked *