using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VoxelBusters.NativePlugins;
using LitJson;
public class NotificationManager: MonoBehaviour
{
public static NotificationManager instanse;
private string deviceToken;
private const int coldLunch = 1;
private const int openByPush = 2;
private const int receivedInOpenedApp = 3;
private bool isLunchedByPush = false;
private bool isDidRegistered = false;
private string pushId = "";
void Awake()
{
if (instanse != null)
{
Destroy(gameObject);
}
else
{
instanse = this;
DontDestroyOnLoad(gameObject);
}
}
void Start()
{
NPBinding.NotificationService.RegisterNotificationTypes(NotificationType.Alert | NotificationType.Badge | NotificationType.Sound);
NPBinding.NotificationService.RegisterForRemoteNotifications();
}
private void OnEnable()
{
NotificationService.DidFinishRegisterForRemoteNotificationEvent += OnFinishedRemoteNotificationRegistration;
NotificationService.DidLaunchWithRemoteNotificationEvent += OnLaunchedWithRemoteNotification;
NotificationService.DidReceiveRemoteNotificationEvent += OnReceivingRemoteNotification;
}
private void OnDisable()
{
NotificationService.DidFinishRegisterForRemoteNotificationEvent -= OnFinishedRemoteNotificationRegistration;
NotificationService.DidLaunchWithRemoteNotificationEvent -= OnLaunchedWithRemoteNotification;
NotificationService.DidReceiveRemoteNotificationEvent -= OnReceivingRemoteNotification;
}
private void OnFinishedRemoteNotificationRegistration(string _deviceToken, string _error)
{
isDidRegistered = true;
if (_error == null)
{
Logger.Debug("push token: " + _deviceToken);
this.deviceToken = _deviceToken;
if (isLunchedByPush)
{
APIManager.instanse.SendPushIdToServer(_deviceToken, pushId, coldLunch);
}
else
{
SendTokenToServer(_deviceToken);
}
}
else
{
SendTokenToServer("0");
Logger.Debug("push error: " + _error);
}
}
private void SendTokenToServer(string token)
{
APIManager.instanse.SendTokenToServer(token);
}
private void OnLaunchedWithRemoteNotification(CrossPlatformNotification _notification)
{
isLunchedByPush = true;
var data = _notification.UserInfo;
pushId = data["pushId"].ToString();
if (isDidRegistered)
{
APIManager.instanse.SendPushIdToServer(deviceToken, pushId, openByPush);
}
AppMetrica.Instance.ReportEvent(MetricaHelper.appOpenedByPush);
}
private void OnReceivingRemoteNotification(CrossPlatformNotification _notification)
{
var data = _notification.UserInfo;
pushId = data["pushId"].ToString();
APIManager.instanse.SendPushIdToServer(deviceToken, pushId, receivedInOpenedApp);
}
}