Unity local push Mobile Notifications

Article directory

      • 1. Android access
        • 1 Introduction
        • 2. Environment
        • 3. Access Push
      • 2. Q&A

1. Android access

1. Introduction

Unity’s official local Push solution, which supports one-time or repeated notifications
The latest version: 2.1.1, supports Unity 2020.3 and above, supports Android5 (API 21) and iOS 10.0+
Official document address

2. Environment

Unity: 2020.3
MobileNotification::2.0.2
Test device: Android12

3. Access Push

  1. Import Package

Unity-Windows-PackageManaget, Packages select Unity Registry, search for Mobile Notifications, click “Install” in the lower right corner. (It has already been installed locally, so it shows Remove)

  1. import namespace
using Unity. Notifications. Android;
  1. Create a notification channel Channel
var channel = new AndroidNotificationChannel()
            {<!-- -->
                Id = m_ChannelID,
                Name = m_ChannelName,
                Importance = Importance. High,
                Description = m_ChannelDescription
            };
AndroidNotificationCenter. RegisterNotificationChannel(channel);

Notification channel (Channel): Android introduces the notification channel notificationchannels to provide a unified system to help users manage notifications. If it is for androido as the target platform, one or more notification channels must be implemented to display notifications to users.

public AndroidNotificationChannel(string id, string name, string description, Importance importance)

Type Name Description
string id Channel ID (remember ID)
string name Channel name
string description Channel description
Importance id Channel importance, generally default Importance.Default
  1. create notification
var notification = new AndroidNotification();
notification.Title = "Title";
notification.Text = "Content";
notification.FireTime = dateTime;
notification.ShouldAutoCancel = true;
notification.SmallIcon = "icon_48";
notification.LargeIcon = "logo2";
notification.IntentData = "{"data":1}"; //Information delivered by notification
notification.Color = Color.red;
AndroidNotificationCenter.SendNotification(notification, m_ChannelID);

Icon Description:
Only icons added to this list or manually added to the “res/drawable” folder can be used for notifications.
Note that not all devices support colored icons.
Small icons must be at least 48x48px and consist only of white pixels on a transparent background.
Large icons must be no smaller than 192x192px and can contain color.

Type Name Description
string Title Notification title
string Text Channel notification Content
DateTime FireTime Send Time
string SmallIcon The small icon in the upper left corner of the notification
Color Color The upper left of the notification Corner small icon background color
string LargeIcon Notification upper right corner large icon
string IntentData Information delivered by notification
bool ShouldAutoCancel Whether to automatically cancel the notification (after clicking the notification, it will be deleted automatically)
  1. Notification icon settings

(1) MobileNotification settings management pictures, as shown in the figure, can generate several large and small pictures, and set the icon logo, when generating the notification, set the icon through the logo

(2) Under the Assets-Plugin-Android-res-drawable folder, put the icon, and set the icon by name when creating a notification

  1. send notification
AndroidNotificationCenter.SendNotification(notification, m_ChannelID)
  1. cancellation notice
//Cancel the specified ID notification
AndroidNotificationCenter.CancelNotification(data.Id);
// cancel all notifications
AndroidNotificationCenter.CancelAllNotifications();
// Cancel all pending notifications
AndroidNotificationCenter.CancelAllScheduledNotifications();
// cancel all notifications that have been displayed
AndroidNotificationCenter.CancelAllDisplayedNotifications();
  1. get notification status
AndroidNotificationCenter.CheckScheduledNotificationStatus(notificationId);
  1. Register notification click callback
AndroidNotificationCenter.OnNotificationReceived + = OnReceivedNotificationHandler;
        /// <summary>
        /// Notification click callback
        /// </summary>
        /// <param name="data"></param>
        private void OnReceivedNotificationHandler(AndroidNotificationIntentData data)
        {<!-- -->
            var msg = "Notification received : " + data.Id + "\
";
            msg + = "\
 Notification received: ";
            msg + = "\
 .Title: " + data.Notification.Title;
            msg + = "\
 .Body: " + data.Notification.Text;
            msg + = "\
 .Channel: " + data.Channel;
            Debug.Log("[MobileNotification] msg: " + msg);

            var notificationIntentData = AndroidNotificationCenter. GetLastNotificationIntent();
            if (notificationIntentData != null)
            {<!-- -->
                var id = notificationIntentData.Id;
                var channel = notificationIntentData. Channel;
                var notification = notificationIntentData. Notification;
                Debug.Log("[MobileNotification] IntentData: " + notification.IntentData);
                Debug.Log("[MobileNotification] CancelNotification: " + id);
            }
            else
            {<!-- -->
                Debug.Log("[MobileNotification] GetLastNotificationIntent failed");
            }
            Debug.Log("[MobileNotification] Status: " + AndroidNotificationCenter.CheckScheduledNotificationStatus(data.Id));
            //AndroidNotificationCenter.CancelNotification(data.Id);
            //ToDo
        }
  1. replacement notice
AndroidNotificationCenter.UpdateScheduledNotification(id, newNotification, "channel_id");
  1. notification display

Second, Q & amp;A

  1. “Sending contentIntent failed: android.app.PendingIntent$CanceledException”

(1) Phenomenon: Click the notification bar, but the APP is not started, and the error message is as shown in the figure
(2) Reason: In order to block the black screen when the game starts, the project extends the Activity of Unity, The configuration in Plugin/Android/AndroidManifest.xml is not the default “com.unity3d.player.UnityPlayerActivity”, resulting in no Activity to be started
(3) Method: “Editor-Project Settings-Mobile Notigficationd”, check “Use Custom Activity”, and fill in the name of the expanded Activity