-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathAppBootstrapper.cs
More file actions
129 lines (105 loc) · 4.71 KB
/
Copy pathAppBootstrapper.cs
File metadata and controls
129 lines (105 loc) · 4.71 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
using OneSignalDemo.Repositories;
using OneSignalDemo.Services;
using OneSignalDemo.ViewModels;
using OneSignalSDK;
using OneSignalSDK.Debug.Models;
using OneSignalSDK.InAppMessages;
using OneSignalSDK.LiveActivities;
using OneSignalSDK.Notifications;
using UnityEngine;
namespace OneSignalDemo
{
public class AppBootstrapper : MonoBehaviour
{
private const string OneSignalAppId = "77e32082-ea27-42e3-a898-c72e141824ef";
private const string Tag = "AppBootstrapper";
[SerializeField]
private AppViewModel _viewModel;
private PreferencesService _prefs;
private OneSignalApiService _apiService;
private OneSignalRepository _repository;
private void Awake()
{
DontDestroyOnLoad(gameObject);
}
private async void Start()
{
_prefs = new PreferencesService();
_apiService = new OneSignalApiService();
_repository = new OneSignalRepository(_apiService);
var appId = _prefs.AppId;
if (string.IsNullOrEmpty(appId))
{
appId = OneSignalAppId;
_prefs.AppId = appId;
}
_apiService.SetAppId(appId);
_apiService.LoadApiKey();
OneSignal.Debug.LogLevel = LogLevel.Verbose;
OneSignal.ConsentRequired = _prefs.ConsentRequired;
OneSignal.ConsentGiven = _prefs.PrivacyConsent;
OneSignal.Initialize(appId);
#if UNITY_IOS
OneSignal.LiveActivities.SetupDefault(
new LiveActivitySetupOptions
{
EnablePushToStart = true,
EnablePushToUpdate = true,
}
);
#endif
OneSignal.InAppMessages.Paused = _prefs.IamPaused;
OneSignal.Location.IsShared = _prefs.LocationShared;
RegisterSdkListeners();
_viewModel.Init(_repository, _prefs);
_viewModel.LoadInitialState();
await _viewModel.LoadInitialDataAsync();
if (!_viewModel.HasPermission)
_viewModel.PromptPush();
_ = TooltipHelper.Instance.InitAsync();
LogManager.Instance.Info(Tag, "App initialized");
}
private void RegisterSdkListeners()
{
OneSignal.InAppMessages.WillDisplay += OnIamWillDisplay;
OneSignal.InAppMessages.DidDisplay += OnIamDidDisplay;
OneSignal.InAppMessages.WillDismiss += OnIamWillDismiss;
OneSignal.InAppMessages.DidDismiss += OnIamDidDismiss;
OneSignal.InAppMessages.Clicked += OnIamClicked;
OneSignal.Notifications.Clicked += OnNotificationClicked;
OneSignal.Notifications.ForegroundWillDisplay += OnNotificationForegroundWillDisplay;
}
private void OnDestroy()
{
if (OneSignal.Default == null)
return;
OneSignal.InAppMessages.WillDisplay -= OnIamWillDisplay;
OneSignal.InAppMessages.DidDisplay -= OnIamDidDisplay;
OneSignal.InAppMessages.WillDismiss -= OnIamWillDismiss;
OneSignal.InAppMessages.DidDismiss -= OnIamDidDismiss;
OneSignal.InAppMessages.Clicked -= OnIamClicked;
OneSignal.Notifications.Clicked -= OnNotificationClicked;
OneSignal.Notifications.ForegroundWillDisplay -= OnNotificationForegroundWillDisplay;
}
private void OnIamWillDisplay(object sender, InAppMessageWillDisplayEventArgs e) =>
LogManager.Instance.Info(Tag, $"IAM will display: {e.Message.MessageId}");
private void OnIamDidDisplay(object sender, InAppMessageDidDisplayEventArgs e) =>
LogManager.Instance.Info(Tag, $"IAM did display: {e.Message.MessageId}");
private void OnIamWillDismiss(object sender, InAppMessageWillDismissEventArgs e) =>
LogManager.Instance.Info(Tag, $"IAM will dismiss: {e.Message.MessageId}");
private void OnIamDidDismiss(object sender, InAppMessageDidDismissEventArgs e) =>
LogManager.Instance.Info(Tag, $"IAM did dismiss: {e.Message.MessageId}");
private void OnIamClicked(object sender, InAppMessageClickEventArgs e) =>
LogManager.Instance.Info(Tag, $"IAM clicked: {e.Result.ActionId}");
private void OnNotificationClicked(object sender, NotificationClickEventArgs e) =>
LogManager.Instance.Info(Tag, $"Notification clicked: {e.Result.ActionId}");
private void OnNotificationForegroundWillDisplay(
object sender,
NotificationWillDisplayEventArgs e
)
{
LogManager.Instance.Info(Tag, "Notification received in foreground");
e.Notification.Display();
}
}
}