-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathOneSignalApiService.cs
More file actions
210 lines (176 loc) · 7.27 KB
/
Copy pathOneSignalApiService.cs
File metadata and controls
210 lines (176 loc) · 7.27 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using OneSignalDemo.Models;
using UnityEngine;
using UnityEngine.Networking;
namespace OneSignalDemo.Services
{
public class OneSignalApiService
{
private string _appId;
private string _apiKey;
private const string NotificationImageUrl =
"https://media.onesignal.com/automated_push_templates/ratings_template.png";
private const string PlaceholderApiKey = "your_rest_api_key";
public void SetAppId(string appId) => _appId = appId;
public string GetAppId() => _appId;
public void LoadApiKey()
{
var envPath = Path.Combine(Application.dataPath, "..", ".env");
#if !UNITY_EDITOR
var streamingPath = Path.Combine(Application.streamingAssetsPath, ".env");
if (File.Exists(streamingPath))
envPath = streamingPath;
#endif
if (!File.Exists(envPath))
return;
foreach (var line in File.ReadAllLines(envPath))
{
var trimmed = line.Trim();
if (trimmed.StartsWith("#") || !trimmed.Contains("="))
continue;
var eqIndex = trimmed.IndexOf('=');
var key = trimmed.Substring(0, eqIndex).Trim();
var value = trimmed.Substring(eqIndex + 1).Trim();
if (key == "ONESIGNAL_API_KEY")
_apiKey = value;
}
}
public bool HasApiKey() =>
!string.IsNullOrEmpty(_apiKey) && _apiKey != PlaceholderApiKey;
public async Task<bool> SendNotification(NotificationType type, string subscriptionId)
{
if (string.IsNullOrEmpty(subscriptionId) || string.IsNullOrEmpty(_appId))
return false;
string title,
body;
switch (type)
{
case NotificationType.Simple:
title = "Simple Notification";
body = "This is a simple push notification";
break;
case NotificationType.WithImage:
title = "Image Notification";
body = "This notification includes an image";
break;
default:
return false;
}
var payload = new JObject
{
["app_id"] = _appId,
["include_subscription_ids"] = new JArray(subscriptionId),
["headings"] = new JObject { ["en"] = title },
["contents"] = new JObject { ["en"] = body },
};
if (type == NotificationType.WithImage)
{
payload["big_picture"] = NotificationImageUrl;
payload["ios_attachments"] = new JObject { ["image"] = NotificationImageUrl };
}
return await PostNotification(payload.ToString());
}
public async Task<bool> SendCustomNotification(
string title,
string body,
string subscriptionId
)
{
if (string.IsNullOrEmpty(subscriptionId) || string.IsNullOrEmpty(_appId))
return false;
var payload = new JObject
{
["app_id"] = _appId,
["include_subscription_ids"] = new JArray(subscriptionId),
["headings"] = new JObject { ["en"] = title },
["contents"] = new JObject { ["en"] = body },
};
return await PostNotification(payload.ToString());
}
public async Task<UserData> FetchUser(string onesignalId)
{
if (string.IsNullOrEmpty(onesignalId) || string.IsNullOrEmpty(_appId))
return null;
var url =
$"https://api.onesignal.com/apps/{_appId}/users/by/onesignal_id/{onesignalId}";
var request = UnityWebRequest.Get(url);
request.SetRequestHeader("Accept", "application/json");
var tcs = new TaskCompletionSource<bool>();
var operation = request.SendWebRequest();
operation.completed += _ => tcs.TrySetResult(true);
await tcs.Task;
UserData result = null;
if (request.result == UnityWebRequest.Result.Success)
{
try
{
result = UserData.FromJson(request.downloadHandler.text);
}
catch (Exception ex)
{
UnityEngine.Debug.LogError($"Error parsing user data: {ex.Message}");
}
}
request.Dispose();
return result;
}
public async Task<bool> UpdateLiveActivity(
string activityId,
string eventType,
JObject eventUpdates = null
)
{
if (string.IsNullOrEmpty(activityId) || string.IsNullOrEmpty(_appId) || !HasApiKey())
return false;
var url =
$"https://api.onesignal.com/apps/{_appId}/live_activities/{activityId}/notifications";
var payload = new JObject
{
["event"] = eventType,
["name"] = "Unity Demo Update",
["priority"] = 10,
};
if (eventUpdates != null)
payload["event_updates"] = eventUpdates;
if (eventType == "end")
{
var unixTimestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
payload["dismissal_date"] = unixTimestamp;
}
var jsonPayload = payload.ToString();
var request = new UnityWebRequest(url, "POST");
var bodyRaw = Encoding.UTF8.GetBytes(jsonPayload);
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
request.downloadHandler = new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
request.SetRequestHeader("Authorization", $"Key {_apiKey}");
var tcs = new TaskCompletionSource<bool>();
var operation = request.SendWebRequest();
operation.completed += _ => tcs.TrySetResult(true);
await tcs.Task;
bool success = request.responseCode >= 200 && request.responseCode < 300;
request.Dispose();
return success;
}
private async Task<bool> PostNotification(string jsonPayload)
{
var request = new UnityWebRequest("https://onesignal.com/api/v1/notifications", "POST");
var bodyRaw = Encoding.UTF8.GetBytes(jsonPayload);
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
request.downloadHandler = new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
request.SetRequestHeader("Accept", "application/vnd.onesignal.v1+json");
var tcs = new TaskCompletionSource<bool>();
var operation = request.SendWebRequest();
operation.completed += _ => tcs.TrySetResult(true);
await tcs.Task;
bool success = request.result == UnityWebRequest.Result.Success;
request.Dispose();
return success;
}
}
}