【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)

TypeNameDescription
stringidChannel ID (记住ID)
stringnameChannel名称
stringdescriptionChannel 描述
ImportanceidChannel重要程度,一般默认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,并且可以包含颜色。

TypeNameDescription
stringTitle通知标题
stringTextChannel通知内容
DateTimeFireTime发送时间
stringSmallIcon通知左上角小图标
ColorColor通知左上角小图标背景颜色
stringLargeIcon通知右上角大图标
stringIntentData通知传递的信息
boolShouldAutoCancel是否自动取消通知(点击通知后,自动删除)
  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名称填入即可
在这里插入图片描述

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值