Skip to content

Commit 66f61d5

Browse files
committed
Adding easier way to construct custom sequences + notification ID print
1 parent fa91223 commit 66f61d5

File tree

1 file changed

+54
-30
lines changed
  • TestProjects/com.unity.mobile-notifications-sample/Assets/Scripts

1 file changed

+54
-30
lines changed

TestProjects/com.unity.mobile-notifications-sample/Assets/Scripts/AndroidTest.cs

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class AndroidTest : MonoBehaviour
2323
private Button ButtonModifyExplicitID;
2424
private Button ButtonCancelExplicitID;
2525
private Button ButtonCheckStatusExplicitID;
26+
private AndroidNotificationTemplate[] AndroidNotificationsTemplates;
2627

2728
public int notificationExplicitID
2829
{
@@ -106,6 +107,32 @@ private void HandleLastNotificationIntent()
106107
m_LOGGER.Red("Notification not found!", 1);
107108
}
108109
}
110+
111+
private AndroidNotification parseNotificationTemplate(AndroidNotificationTemplate template)
112+
{
113+
AndroidNotification newNotification = new AndroidNotification()
114+
{
115+
Title = template.Title,
116+
Text = template.Text,
117+
118+
SmallIcon = template.SmallIcon,
119+
LargeIcon = template.LargeIcon,
120+
Style = template.NotificationStyle,
121+
FireTime = System.DateTime.Now.AddSeconds(template.FireInSeconds),
122+
Color = template.Color,
123+
Number = template.Number,
124+
ShouldAutoCancel = template.ShouldAutoCancel,
125+
UsesStopwatch = template.UsesStopWatch,
126+
Group = template.Group,
127+
GroupSummary = template.GroupSummary,
128+
SortKey = template.SortKey,
129+
IntentData = template.IntentData,
130+
ShowTimestamp = template.ShowTimestamp,
131+
RepeatInterval = TimeSpan.FromSeconds(template.RepeatInterval),
132+
//ShowInForeground = template.ShowInForeground
133+
};
134+
return newNotification;
135+
}
109136

110137
private void InstantiateAllTestButtons()
111138
{
@@ -123,40 +150,16 @@ private void InstantiateAllTestButtons()
123150
m_groups["Modify"]["Modify pending Explicit notification"] = new Action(() => { ModifyExplicitNotification(); });
124151
m_groups["Modify"]["Cancel pending Explicit notification"] = new Action(() => { CancelExplicitNotification(); });
125152
m_groups["Modify"]["Check status of Explicit notification"] = new Action(() => { CheckStatusOfExplicitNotification (); });
126-
127-
153+
AndroidNotificationsTemplates = Resources.LoadAll<AndroidNotificationTemplate>("AndroidNotifications");
128154
m_groups["Send"] = new OrderedDictionary();
129-
foreach (AndroidNotificationTemplate template in Resources.LoadAll("AndroidNotifications", typeof(AndroidNotificationTemplate)))
155+
foreach (AndroidNotificationTemplate template in AndroidNotificationsTemplates)
130156
{
131157
if (template == null) continue;
132-
m_groups["Send"][$"[{template.FireInSeconds}s] {template.ButtonName}"] = new Action(() => {
133-
SendNotification(
134-
new AndroidNotification()
135-
{
136-
Title = template.Title,
137-
Text = template.Text,
138-
139-
SmallIcon = template.SmallIcon,
140-
LargeIcon = template.LargeIcon,
141-
Style = template.NotificationStyle,
142-
FireTime = System.DateTime.Now.AddSeconds(template.FireInSeconds),
143-
Color = template.Color,
144-
Number = template.Number,
145-
ShouldAutoCancel = template.ShouldAutoCancel,
146-
UsesStopwatch = template.UsesStopWatch,
147-
Group = template.Group,
148-
GroupSummary = template.GroupSummary,
149-
SortKey = template.SortKey,
150-
IntentData = template.IntentData,
151-
ShowTimestamp = template.ShowTimestamp,
152-
RepeatInterval = TimeSpan.FromSeconds(template.RepeatInterval),
153-
ShowInForeground = template.ShowInForeground,
154-
},
155-
template.Channel, template.NotificationID
156-
);
158+
m_groups["Send"][$"[{template.FireInSeconds}s] {template.ButtonName}"] = new Action(() =>
159+
{
160+
SendNotification(parseNotificationTemplate(template), template.Channel, template.NotificationID);
157161
});
158162
}
159-
160163
m_groups["Cancellation"] = new OrderedDictionary();
161164
m_groups["Cancellation"]["Cancel all notifications"] = new Action(() => { CancelAllNotifications(); });
162165
m_groups["Cancellation"]["Cancel pending notifications"] = new Action(() => { CancelPendingNotifications(); });
@@ -209,7 +212,8 @@ private void InstantiateAllTestButtons()
209212
AndroidNotificationCenter.OpenNotificationSettings("secondary_channel");
210213
});
211214
m_groups["Channels"]["Delete All Channels"] = new Action(() => { DeleteAllChannels(); });
212-
215+
m_groups["Custom sequences"] = new OrderedDictionary();
216+
m_groups["Custom sequences"]["Run custom sequence"] = new Action(() => { StartCoroutine(RunCustomSequence()); });
213217
foreach (KeyValuePair<string, OrderedDictionary> group in m_groups)
214218
{
215219
// Instantiate group
@@ -288,6 +292,7 @@ public void SendNotification(AndroidNotification notification, string channel =
288292
}
289293
if (notificationID != 0)
290294
{
295+
notification.Text = "ID: " + notificationID + " " + notification.Text;
291296
AndroidNotificationCenter.SendNotificationWithExplicitID(notification, channel, notificationID);
292297
notificationExplicitID = notificationID;
293298
}
@@ -353,6 +358,25 @@ public void ScrollLogsToBottom()
353358
Canvas.ForceUpdateCanvases();
354359
m_gameObjectReferences.LogsScrollRect.verticalNormalizedPosition = 0f;
355360
}
361+
362+
IEnumerator RunCustomSequence()
363+
{
364+
AndroidNotificationTemplate ant = AndroidNotificationsTemplates[4];
365+
SendNotification(parseNotificationTemplate(ant), ant.Channel, 15);
366+
SendNotification(parseNotificationTemplate(ant), ant.Channel, 20);
367+
SendNotification(parseNotificationTemplate(ant), ant.Channel, 28);
368+
SendNotification(parseNotificationTemplate(ant), ant.Channel, 14);
369+
yield return new WaitForSeconds(ant.FireInSeconds+5);
370+
AndroidNotificationCenter.CancelNotification(15);
371+
yield return new WaitForSeconds(5);
372+
AndroidNotificationCenter.CancelDisplayedNotification(20);
373+
yield return new WaitForSeconds(5);
374+
SendNotification(parseNotificationTemplate(ant), ant.Channel, 99);
375+
SendNotification(parseNotificationTemplate(ant), ant.Channel, 99);
376+
yield return new WaitForSeconds(ant.FireInSeconds-1);
377+
SendNotification(parseNotificationTemplate(ant), ant.Channel, 99);
378+
SendNotification(parseNotificationTemplate(ant), ant.Channel, 99);
379+
}
356380

357381
#endif
358382
}

0 commit comments

Comments
 (0)