现在手游中出现了越多的定时通知的需求,因为是固定时间推送,所以不需要通过服务器进行推送,只需要利用各个平台自己的本地推送机制就可以实现这个功能。
在Unity3D官方提供的API中,UnityEngine.iOS中有一个类:LocalNotification
它只实现了iOS的本地推送,即仅适用于iphone/ipad/ipod Touch.
Unity3D API链接:https://docs.unity3d.com/ScriptReference/iOS.LocalNotification.html
之前看过一篇文章,有关iOS本地推送的文章,可以看看。
iOS本地推送通知链接:http://www.xuanyusong.com/archives/2632
对于Android端,Unity3D并没有提供现成的接口,所以使用GitHub上的第三方库中实现的接口
GitHub地址:https://github.com/Agasper/unity-android-notifications
我们将这个第三方库clone到本地的一个自建文件夹,打开后有两个文件夹:branches和trunk,使用Unity3D打开trunk中的UnityProject。然后打包,在手机上进行测试,可以实现本地推送。
下面看看其中的接口方法:
public static int SendNotification(TimeSpan delay, string title, string message, Color32 bgColor, bool sound = true, bool vibrate = true, bool lights = true, string bigIcon = "", String soundName = null, string channel = "default", params Action[] actions)
{
int id = new System.Random().Next();
return SendNotification(id, (long)delay.TotalSeconds * 1000, title, message, bgColor, sound, vibrate, lights, bigIcon, soundName, channel, actions);
}
public static int SendNotification(int id, TimeSpan delay, string title, string message, Color32 bgColor, bool sound = true, bool vibrate = true, bool lights = true, string bigIcon = "", String soundName = null, string channel = "default", params Action[] actions)
{
return SendNotification(id, (long)delay.TotalSeconds * 1000, title, message, bgColor, sound, vibrate, lights, bigIcon, soundName, channel, actions);
}
public static int SendNotification(int id, long delayMs, string title, string message, Color32 bgColor, bool sound = true, bool vibrate = true, bool lights = true, string bigIcon = "", String soundName = null, string channel = "default", params Action[] actions)
{
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidJavaClass pluginClass = new AndroidJavaClass(fullClassName);
if (pluginClass != null)
{
pluginClass.CallStatic("SetNotification", id, delayMs, title, message, message,
sound ? 1 : 0, soundName, vibrate ? 1 : 0, lights ? 1 : 0, bigIcon, "notify_icon_small",
ToInt(bgColor), bundleIdentifier, channel, PopulateActions(actions));
}
return id;
#elif UNITY_IOS && !UNITY_EDITOR
iOSNotification notification = new iOSNotification();
notification.identifier = id;
notification.message = message;
notification.delay = ((double) delayMs) / 1000.0;
notification.repeat = 0;
notification.category = channel;
notification.sound = sound;
notification.soundName = soundName;
SetActions(ref notification, actions);
scheduleNotification(ref notification);
return id;
#else
return 0;
#endif
}
我们来看一下这三个重载方法:SendNotification,前两个是对第三个的封装,区别就只是参数:
int id //notification的编号
TimeSpan delay //延迟时间(5000,就是5秒后触发)
string title //推送内容标题
string message //推送内容
Color32 bgColor //Unity提供的API,以32位格式表示RGBA颜色
bool sound = true //声音
bool vibrate = true //颤动
bool lights = true //闪亮
string bigIcon = “” //缺省值
String soundName = null //声音文件名
string channel = “default” //都同意给false,表示本地推送(我理解的)
params Action[] actions //params表示函数的参数是可变个数的。一个table
我们来看项目中给到的测试代码(NotificationTest.cs):
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class NotificationTest : MonoBehaviour
{
public InputField m_input;
void Awake()
{
LocalNotification.ClearNotifications();
}
public void OneTime()
{
long delay = long.Parse(m_input.text); //string转换为long
LocalNotification.SendNotification(1, delay, "Title", "Long message text", new Color32(0xff, 0x44, 0x44, 255));
}
public void Time() //我自己加的,使用InputField输入时间戳,然后在指定时间推送
{
long delay = long.Parse(m_input.text);
LocalNotification.notification(1, delay, "Title", "Long message text with big icon", new Color32(0xff, 0x44, 0x44, 255), true, true, true, "app_icon");
m_input.text = "hello";
}
public void OneTimeBigIcon()
{
LocalNotification.SendNotification(1, 5000, "Title", "Long message text with big icon", new Color32(0xff, 0x44, 0x44, 255), true, true, true, "app_icon");
}
public void OneTimeWithActions()
{
LocalNotification.Action action1 = new LocalNotification.Action("background", "In Background", this);
action1.Foreground = false;
LocalNotification.Action action2 = new LocalNotification.Action("foreground", "In Foreground", this);
LocalNotification.SendNotification(1, 5000, "Title", "Long message text with actions", new Color32(0xff, 0x44, 0x44, 255), true, true,