Unity3D游戏中Android本地推送通知整理
public static int SendNotification(TimeSpan delay, string title, string message, Color32 bgColor, bool sound = true, bool vibrate = true, bool lights = {
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 {
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 = {
#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.delay = ((double) delayMs) / 1000.0;
notification.category = channel;
notification.sound = sound;
notification.soundName = soundName;
SetActions(ref notification, actions);
scheduleNotification(ref notification);
return id;
#el
return0;
#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” //都同意给fal,表⽰本地推送(我理解的)
到处的反义词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.Par(); //string转换为long
LocalNotification.SendNotification(1, delay, "Title", "Long message text", new Color32(0xff, 0x44, 0x44, 255));
清清的小河}
public void Time() //我⾃⼰加的,使⽤InputField输⼊时间戳,然后在指定时间推送
{
long delay = long.Par();
= "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 = fal;
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, true, null, }
public void Repeating()
{
LocalNotification.SendRepeatingNotification(1, 5000, 60000, "Title", "Long message text", new Color32(0xff, 0x44, 0x44, 255));
}
public void Stop()
{
LocalNotification.CancelNotification(1);
}
public void OnAction(string identifier)
{
Debug.Log("Got action " + identifier);
}
}
LocalNotification.cs
using System;
using System.IO;
using UnityEngine;
#if UNITY_IOS
using System.Runtime.InteropServices;
#endif
using System.Collections.Generic;
public class LocalNotification
{
#if UNITY_ANDROID && !UNITY_EDITOR
private static string fullClassName = "net.agasper.unitynotification.UnityNotificationManager";
private static string actionClassName = "net.agasper.unitynotification.NotificationAction";
#endif
#if UNITY_5_6_OR_NEWER
private static string bundleIdentifier { get { return Application.identifier; } }
#el
private static string bundleIdentifier { get { return Application.bundleIdentifier; } }
#endif
public static void notification(int id, long time, string title, string message,Color32 bgColor, bool sound = true, bool vibrate = true, bool lights = true
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); //当地时区
DateTime dt = startTime.AddMilliconds(time);
TimeSpan Timespan = dt - DateTime.Now;
long delay = Timespan.Days * 24 * 3600 * 1000 + Timespan.Hours * 3600 * 1000 + Timespan.Minutes * 60 * 1000 + Timespan.Seconds * 1000
SendNotification(id, delay, title,message, bgColor, sound, vibrate, lights, bigIcon, soundName, channel, actions);
}
public static int SendNotification(TimeSpan delay, string title, string message, Color32 bgColor, bool sound = true, bool vibrate = true, bool lights =
{
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
{
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 =
{
#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.delay = ((double) delayMs) / 1000.0;
notification.category = channel;
notification.sound = sound;
notification.soundName = soundName;
SetActions(ref notification, actions);
scheduleNotification(ref notification);
return id;
#el
return0;
#endif
}
public static int SendRepeatingNotification(TimeSpan delay, TimeSpan timeout, string title, string message, Color32 bgColor, bool sound = true,
{
int id = new System.Random().Next();
return SendRepeatingNotification(id, (long)delay.TotalSeconds * 1000, (int)timeout.TotalSeconds, title, message, bgColor, sound, vibrate, lights, bigIco
return SendRepeatingNotification(id, (long)delay.TotalSeconds * 1000, (int)timeout.TotalSeconds, title, message, bgColor, sound, vibrate, lights, bigIco }
public static int SendRepeatingNotification(int id, TimeSpan delay, TimeSpan timeout, string title, string message, Color32 bgColor, bool sound =
{
return SendRepeatingNotification(id, (long)delay.TotalSeconds * 1000, (int)timeout.TotalSeconds, title, message, bgColor, sound, vibrate, lights, bigIco }
public static int SendRepeatingNotification(int id, long delayMs, long timeoutMs, string title, string message, Color32 bgColor, bool sound = true,
{
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidJavaClass pluginClass = new AndroidJavaClass(fullClassName);
if (pluginClass != null)
{
pluginClass.CallStatic("SetRepeatingNotification", id, delayMs, title, message, message, timeoutMs,
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.delay = ((double) delayMs) / 1000.0;
notification.category = channel;
notification.sound = sound;
notification.soundName = soundName;
SetActions(ref notification, actions);
scheduleNotification(ref notification);
return id;
#el
return0;
#endif
}
public static void CancelNotification(int id)
{
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidJavaClass pluginClass = new AndroidJavaClass(fullClassName);
if (pluginClass != null) {
pluginClass.CallStatic("CancelPendingNotification", id);
}
#elif UNITY_IOS && !UNITY_EDITOR
cancelNotification(id);
#endif
}
public static void ClearNotifications()
{
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidJavaClass pluginClass = new AndroidJavaClass(fullClassName);
if (pluginClass != null) {
pluginClass.CallStatic("ClearShowingNotifications");
}
#elif UNITY_IOS && !UNITY_EDITOR
cancelAllNotifications();
#endif
}
/// This allows you to create a custom channel for different kinds of notifications.
/// Channels are required on Android Oreo and above. If you don't call this method, a channel will be created for you with the configuration you give to Se public static void CreateChannel(string identifier, string name, string description, Color32 lightColor, bool enableLights = true, string soundName =
{
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidJavaClass pluginClass = new AndroidJavaClass(fullClassName);
if (pluginClass != null)
{
pluginClass.CallStatic("CreateChannel", identifier, name, description, (int) importance, soundName, enableLights ? 1 : 0, ToInt(lightColor), vibrate ? }
#endif
}
public enum Importance
{
/// Default notification importance: shows everywhere, makes noi, but does not visually intrude.
Default = 3,
/
// Higher notification importance: shows everywhere, makes noi and peeks. May u full screen intents.
High = 4,
/// Low notification importance: shows everywhere, but is not intrusive.
Low = 2,
/// Unud.
Max = 5,
/// Min notification importance: only shows in the shade, below the fold. This should not be ud with Service.startForeground since a foreground rvi Min = 1,
/// A notification with no importance: does not show in the shade.
None = 0
}
public class Action
{
public Action(string identifier, string title, MonoBehaviour handler)
{
this.Identifier = identifier;
this.Title = title;
if (handler != null)
{
this.GameObject = handler.gameObject.name;
this.HandlerMethod = "OnAction";
}
}
public string Identifier;
public string Title;
public string Icon;
public bool Foreground = true;
施娅娅public string GameObject;
public string HandlerMethod;
}
private static int ToInt(Color32 color)
{
return color.r * 65536 + color.g * 256 + color.b;
}
#if UNITY_ANDROID && !UNITY_EDITOR
private static AndroidJavaObject PopulateActions(Action[] actions)
{
AndroidJavaObject actionList = null;
if (actions.Length > 0)
{
actionList = new AndroidJavaObject("java.util.ArrayList");
for (int i = 0; i < actions.Length; i++)
{
var action = actions[i];
using (AndroidJavaObject notificationObject = new AndroidJavaObject(actionClassName))