JD2022-TU1/main/phone_apps/JDCompanionApp/Assets/Scripts/SystemUI/NotificationManager.cs

233 lines
7.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.IO;
#if UNITY_IOS
using System;
using System.Runtime.InteropServices;
#endif
using Analytics;
using UnityEngine;
using UnityEngine.Networking;
using UTNotifications;
namespace SystemUI
{
class NotificationManager: MonoBehaviour
{
#if UNITY_IOS
[DllImport("__Internal")]
[return: MarshalAs(UnmanagedType.LPStr)]
private static extern string LanguageInfo_getLanguageIdentifier();
[DllImport("__Internal")]
[return: MarshalAs(UnmanagedType.LPStr)]
private static extern string TimeZoneInfo_getTimeZone();
//[DllImport("__Internal")]
//private static extern void DebugLogToNative_displayLog([MarshalAs(UnmanagedType.LPStr)] string imageName);
#endif
private const string BADGE_RESET_SENT = "badgeResetSent";
public static string registrationID = "";
private void OnSendRegistrationId(string providerName, string registrationId)
{
// houston
Debug.Log("### OnSendRegistrationId " + providerName + ", " + registrationId);
registrationID = registrationId;
var udid = UbimobileToolkit.instance.ubisoftDeviceUniqueId();
if (Debug.isDebugBuild)
{
Debug.Log("NotificationManager UDID : " + udid);
WriteToFile("udid: " + udid);
}
var url = "https://gamecfg-mob.ubi.com/profile/?action=register";
url += "&productid=632";
url += "&deviceuid=" + udid;
// TODO Replace that because it's being null on iOS
#pragma warning disable CS0618 // Type or member is obsolete
//url += "&forwarded_for" + NetworkManager.singleton.networkAddress;
#pragma warning restore CS0618 // Type or member is obsolete
var lang = string.Empty;
var timezone = string.Empty;
#if UNITY_ANDROID
using (AndroidJavaClass locale = new AndroidJavaClass("java.util.Locale"))
{
lang = locale.CallStatic<AndroidJavaObject>("getDefault").Call<string>("getLanguage");
url += "&lang=" + lang;
}
using (AndroidJavaClass locale = new AndroidJavaClass("java.util.TimeZone"))
{
timezone = locale.CallStatic<AndroidJavaObject>("getDefault").Call<string>("getID");
url += "&timezone=" + timezone;
}
#elif UNITY_IOS
lang = LanguageInfo_getLanguageIdentifier();
url += "&lang=" + lang;
timezone = TimeZoneInfo_getTimeZone();
url += "&timezone=" + timezone;
url += "&pushcount=0";
url += "&pushbadge=enabled";
#endif
Debug.Log("timezone:" + timezone);
Debug.Log("lang:" + lang);
if (providerName == "GooglePlay" || providerName == "FCM" || providerName =="ADM" || providerName == "WNS")
{
url += "&gcm_regid=" + registrationId;
}
else if (providerName == "iOS" || providerName == "APNS")
{
url += "&devicetoken=" + registrationId;
}
else
{
Debug.LogError("NotificationManager : Provider not found " + providerName);
return;
}
var request = new UnityWebRequest(url);
Debug.Log("Sending UDID " + udid + " to Houston!");
StartCoroutine(WaitForHoustonRegistration(request));
}
private IEnumerator WaitForHoustonRegistration(UnityWebRequest request)
{
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
{
Debug.LogError("Failed to register device to Houston for PN (" + request.error + ")");
}
else
{
Debug.Log("Device registered for PN!!!");
#if UNITY_IOS
PlayerPrefs.SetInt(BADGE_RESET_SENT, 1);
#endif
}
request.Dispose();
request = null;
}
private void OnNotificationClicked(UTNotifications.ReceivedNotification notification)
{
var id = string.Empty;
if (notification.userData.ContainsKey("id"))
{
// Houston send the id in userData
id = notification.userData["id"];
}
// deep link
Debug.Log("### Notification clicked! text: " + notification.text + ", userId = " + id + ", id = " + notification.id + ", userData = " + notification.userData.Count + ", notificationProfile = " + notification.notificationProfile);
foreach (var data in notification.userData)
{
Debug.Log("Key = " + data.Key + ", value = " + data.Value);
}
Statistics.PushNotificationLaunch(id, notification.title);
}
private void OnNotificationsReceived(IList<ReceivedNotification> receivedNotificationList)
{
Debug.Log("### Notification received!");
var id = string.Empty;
var notificationTitle = string.Empty;
if (receivedNotificationList != null && receivedNotificationList.Count > 0)
{
var receivedNotification = receivedNotificationList[receivedNotificationList.Count - 1];
Debug.Log("### Notification received! text: " + receivedNotification.text + ", id = " + receivedNotification);
notificationTitle = receivedNotification.title;
if (receivedNotification.userData.ContainsKey("id"))
{
// Houston send the id in userData
id = receivedNotification.userData["id"];
}
}
Statistics.PushNotificationLaunch(id, notificationTitle);
}
void Awake()
{
UTNotifications.Manager.Instance.OnSendRegistrationId += OnSendRegistrationId;
UTNotifications.Manager.Instance.OnNotificationClicked += OnNotificationClicked;
UTNotifications.Manager.Instance.OnNotificationsReceived += OnNotificationsReceived;
var result = UTNotifications.Manager.Instance.Initialize(false);
Debug.Log("### Notification initialize -> " + result);
}
void OnDestroy()
{
UTNotifications.Manager.Instance.OnSendRegistrationId -= OnSendRegistrationId;
UTNotifications.Manager.Instance.OnNotificationClicked -= OnNotificationClicked;
}
public static void WriteToFile(string data)
{
var sr = File.CreateText(Application.persistentDataPath + "/deviceIDs.txt");
sr.WriteLine(data);
sr.Close();
}
#if UNITY_IOS
void OnApplicationPause(bool pause)
{
if (pause == false)
{
ResetAppBadgeIcon();
}
}
void ResetAppBadgeIcon()
{
if (UTNotifications.Manager.Instance.GetBadge() > 0)
{
UTNotifications.Manager.Instance.SetBadge(0);
PlayerPrefs.SetInt(BADGE_RESET_SENT, 0);
}
if (PlayerPrefs.GetInt(BADGE_RESET_SENT,0) == 0) // when local badge is reset we always send the register command to Houston to reset the badge counter on server
OnSendRegistrationId("iOS", EncodeRegistrationId(UnityEngine.iOS.NotificationServices.deviceToken));
}
private static string EncodeRegistrationId(byte[] token)
{
if(token == null) {
Debug.LogError("Notification Manager : Token is null");
return string.Empty;
}
return ByteArrayToHexString(token);
}
private static string ByteArrayToHexString(byte[] byteArray)
{
string hex = BitConverter.ToString(byteArray);
return hex.Replace("-", "").ToLower();
}
#endif
}
}