using System Collections using System Collections Generic using UnityE

  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
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);
}
}