Unity 本地推送 Mobile Notifications

一、Android接入

1. 简介

Unity 官方本地Push解决方案,支持一次性或重复通知
最新版本:2.1.1,支持Unity 2020.3及以上版本,支持Android5(API 21)和iOS 10.0+
官方文档地址

2.环境

Unity:2020.3
MobileNotification::2.0.2
测试设备:Android12

3.接入Push

  1. 导入Package

Unity-Windows-PackageManaget,Packages选择Unity Registry,搜索Mobile Notifications,右下角点击"Install"。(本地已经安装过了,故显示Remove)
在这里插入图片描述

  1. 引入命名空间
using Unity.Notifications.Android;
  1. 创建通知渠道Channel
var channel = new AndroidNotificationChannel()
            {
    
    
                Id = m_ChannelID,
                Name = m_ChannelName,
                Importance = Importance.High,
                Description = m_ChannelDescription
            };
AndroidNotificationCenter.RegisterNotificationChannel(channel);

通知渠道(Channel):Android引入了通知渠道notificationchannels以提供统一的系统来帮助用户管理通知如果是针对androido为目标平台时必须实现一个或者多个通知渠道以向用户显示通知 。

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

Type Name Description
string id Channel ID (记住ID)
string name Channel名称
string description Channel 描述
Importance id Channel重要程度,一般默认Importance.Default
  1. 创建通知
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}";   //通知传递的信息
notification.Color = Color.red;
AndroidNotificationCenter.SendNotification(notification, m_ChannelID);

图标说明:
只有添加到此列表或手动添加到“res/drawable”文件夹的图标才能用于通知。
注意,并非所有设备都支持彩色图标。
小图标必须至少为48x48px,并且只由透明背景上的白色像素组成。
大图标必须不小于192x192px,并且可以包含颜色。

Type Name Description
string Title 通知标题
string Text Channel通知内容
DateTime FireTime 发送时间
string SmallIcon 通知左上角小图标
Color Color 通知左上角小图标背景颜色
string LargeIcon 通知右上角大图标
string IntentData 通知传递的信息
bool ShouldAutoCancel 是否自动取消通知(点击通知后,自动删除)
  1. 通知图标设置

(1)MobileNotification设置管理图片,如图所示,可以生成若干大图和小图,并设置图标标识,在生成通知时,通过该标识设置图标
在这里插入图片描述
(2)Assets-Plugin-Android-res-drawable文件夹下,放入图标,创建通知时,通过名称设置图标
在这里插入图片描述

  1. 发送通知
AndroidNotificationCenter.SendNotification(notification, m_ChannelID)
  1. 取消通知
//取消指定ID通知
AndroidNotificationCenter.CancelNotification(data.Id);
//取消全部通知
AndroidNotificationCenter.CancelAllNotifications();
//取消所有未完成的通知
AndroidNotificationCenter.CancelAllScheduledNotifications();
//取消所有已经显示的通知
AndroidNotificationCenter.CancelAllDisplayedNotifications();
  1. 获取通知状态
AndroidNotificationCenter.CheckScheduledNotificationStatus(notificationId);
  1. 注册通知点击回调
AndroidNotificationCenter.OnNotificationReceived += OnReceivedNotificationHandler;
        /// <summary>
        /// 通知点击回调
        /// </summary>
        /// <param name="data"></param>
        private void OnReceivedNotificationHandler(AndroidNotificationIntentData data)
        {
    
    
            var msg = "Notification received : " + data.Id + "\n";
            msg += "\n Notification received: ";
            msg += "\n .Title: " + data.Notification.Title;
            msg += "\n .Body: " + data.Notification.Text;
            msg += "\n .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. 替换通知
AndroidNotificationCenter.UpdateScheduledNotification(id, newNotification, "channel_id");
  1. 通知展示
    在这里插入图片描述

二、Q&A

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

(1)现象: 点击通知栏,没有启动APP,错误信息如图
在这里插入图片描述(2)原因: 项目为了遮挡游戏启动黑屏,扩展Unity的Activity,Plugin/Android/AndroidManifest.xml中配置的并不是默认的"com.unity3d.player.UnityPlayerActivity",导致找不到要启动的Activity
(3)方法: “Editor-Project Settings-Mobile Notigficationd”,勾选"Use Custom Activity",并将扩展后的Activity名称填入即可
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42186644/article/details/129748101