Last active
February 5, 2019 09:28
-
-
Save baobao/a3e004e796ea7a58b8b882193ae000dd to your computer and use it in GitHub Desktop.
MobileNotificationsサンプルコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if UNITY_ANDROID | |
using Unity.Notifications.Android; | |
#elif UNITY_IOS | |
using Unity.Notifications.iOS; | |
#endif | |
/// <summary> | |
/// ローカルプッシュ通知 | |
/// </summary> | |
public static class LocalNotificationWrapper | |
{ | |
private static bool _isInitialized; | |
#if UNITY_ANDROID | |
// 通知チャンネルID | |
// 本サンプルはシングルチャンネルです | |
private static string ChannelId = "channelId"; | |
#endif | |
public static void InitializeIfNeed() | |
{ | |
if (_isInitialized) | |
{ | |
return; | |
} | |
_isInitialized = true; | |
#if UNITY_ANDROID | |
// 通知チャンネルの登録 | |
AndroidNotificationCenter.RegisterNotificationChannel( | |
new AndroidNotificationChannel | |
{ | |
Id = ChannelId, | |
Name = "Default ChannelName", | |
Importance = Importance.High, | |
Description = "Channel Description", | |
// 1を指定してもバッジがつかない... | |
Numbrer = 1, | |
}); | |
#endif | |
} | |
/// <summary> | |
/// ローカル通知の予約 | |
/// </summary> | |
public static void ReserveNotification( | |
string title, | |
string body, | |
int afterSec | |
) | |
{ | |
InitializeIfNeed(); | |
#if UNITY_ANDROID | |
// 通知を送信する | |
AndroidNotificationCenter.SendNotification(new AndroidNotification | |
{ | |
Title = title, | |
Text = body, | |
// アイコンをそれぞれセット | |
SmallIcon = "icon_0", | |
LargeIcon = "icon_1", | |
// 今から何秒後に通知をするか? | |
FireTime = System.DateTime.Now.AddSeconds(afterSec) | |
}, ChannelId); | |
#endif | |
#if UNITY_IOS | |
iOSNotificationCenter.ScheduleNotification(new iOSNotification() | |
{ | |
Title = title, | |
Body = body, | |
ShowInForeground = true, | |
Badge = 1, | |
// 時間をトリガーにする | |
Trigger = new iOSNotificationTimeIntervalTrigger() | |
{ | |
TimeInterval = new System.TimeSpan(0, 0, afterSec), | |
Repeats = false | |
} | |
}); | |
#endif | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment