-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotificationManager.cs
88 lines (77 loc) · 2.6 KB
/
NotificationManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using UnityEngine;
using System;
using UnityEngine.UI;
using TMPro;
#if UNITY_ANDROID
using Unity.Notifications.Android;
using UnityEngine.Android;
#elif UNITY_IOS
using Unity.Notifications.iOS;
#endif
public class NotificationManager : MonoBehaviour
{
public Button SendNotifcationBtn;
public TMP_InputField tittleInputField;
public TMP_InputField messageInputField;
public TMP_InputField timeInputField;
private void Start()
{
#if UNITY_ANDROID
if (!Permission.HasUserAuthorizedPermission("android.permission.POST_NOTIFICATIONS"))
{
Permission.RequestUserPermission("android.permission.POST_NOTIFICATIONS");
}
var channel = new AndroidNotificationChannel()
{
Id = "channel_id",
Name = "Default Channel",
Importance = Importance.Default,
Description = "Generic notifications",
};
AndroidNotificationCenter.RegisterNotificationChannel(channel);
#endif
SendNotifcationBtn.onClick.AddListener(OnSendNotication);
//SendNotification("Test Notification", "This is a test notification", 10);
}
public void OnSendNotication()
{
if (string.IsNullOrEmpty(timeInputField.text)) return;
float time = float.Parse(timeInputField.text);
string title = (string.IsNullOrEmpty(tittleInputField.text)) ? "Test Notification" : tittleInputField.text;
string message = (string.IsNullOrEmpty(messageInputField.text)) ? $"Notifications in {time} minutes" : messageInputField.text;
SendNotification(title, message, time);
}
public void SendNotification(string title, string message, float time)
{
#if UNITY_ANDROID
var AndriodNotification = new AndroidNotification()
{
Title = title,
Text = message,
SmallIcon = "icon",
LargeIcon = "logo",
FireTime = DateTime.Now.AddMinutes(time),
};
AndroidNotificationCenter.SendNotification(AndriodNotification, "channel_id");
#elif UNITY_IOS
var timeTrigger = new iOSNotificationTimeIntervalTrigger()
{
TimeInterval = TimeSpan.FromMinutes(time),
Repeats = false
};
var notification = new iOSNotification()
{
Identifier = "_notification_01",
Title = title,
Body = message,
Subtitle = "Test Subtitle",
ShowInForeground = true,
ForegroundPresentationOption = (PresentationOption.Alert | PresentationOption.Sound),
CategoryIdentifier = "category_id",
ThreadIdentifier = "thread_id",
Trigger = timeTrigger,
};
iOSNotificationCenter.ScheduleNotification(notification);
#endif
}
}