-
Notifications
You must be signed in to change notification settings - Fork 40
/
PushDemoManager.cs
123 lines (106 loc) · 3.88 KB
/
PushDemoManager.cs
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
using HmsPlugin;
using HuaweiMobileServices.Push;
using HuaweiMobileServices.Utils;
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class PushDemoManager : MonoBehaviour
{
private string pushToken;
private Text remoteMessageText, tokenText;
private void Awake()
{
remoteMessageText = GameObject.Find("RemoteMessageText").GetComponent<Text>();
tokenText = GameObject.Find("PushToken").GetComponent<Text>();
}
void Start()
{
/*
* When using multiple kits, we recommend initializing the push kit with the coroutine.
*/
Debug.Log("[HMS] Push Start");
StartCoroutine(LateStart(0f));
}
IEnumerator LateStart(float waitTime)
{
yield return new WaitForSeconds(waitTime);
HMSPushKitManager.Instance.OnTokenSuccess = OnNewToken;
HMSPushKitManager.Instance.OnTokenFailure = OnTokenError;
HMSPushKitManager.Instance.OnTokenBundleSuccess = OnNewToken;
HMSPushKitManager.Instance.OnTokenBundleFailure = OnTokenError;
HMSPushKitManager.Instance.OnMessageSentSuccess = OnMessageSent;
HMSPushKitManager.Instance.OnSendFailure = OnSendError;
HMSPushKitManager.Instance.OnMessageDeliveredSuccess = OnMessageDelivered;
HMSPushKitManager.Instance.OnMessageReceivedSuccess = OnMessageReceived;
HMSPushKitManager.Instance.OnNotificationMessage = OnNotificationMessage;
HMSPushKitManager.Instance.NotificationMessageOnStart = NotificationMessageOnStart;
HMSPushKitManager.Instance.Init();
}
private void OnNotificationMessage(NotificationData data)
{
Debug.Log("[HMSPushDemo] CmdType: " + data.CmdType);
Debug.Log("[HMSPushDemo] MsgId: " + data.MsgId);
Debug.Log("[HMSPushDemo] NotifyId: " + data.NotifyId);
Debug.Log("[HMSPushDemo] KeyValueJSON: " + data.KeyValueJSON);
}
private void NotificationMessageOnStart(NotificationData data)
{
Debug.Log("[HMSPushDemo] CmdType: " + data.CmdType);
Debug.Log("[HMSPushDemo] MsgId: " + data.MsgId);
Debug.Log("[HMSPushDemo] NotifyId: " + data.NotifyId);
Debug.Log("[HMSPushDemo] KeyValueJSON: " + data.KeyValueJSON);
/* TODO: Make your own logic here
* notificationDataOnStart = data;
*/
}
public void OnNewToken(string token)
{
Debug.Log($"[HMS] Push token from OnNewToken is {token}");
if (!string.IsNullOrWhiteSpace(token))
{
pushToken = token;
tokenText.text = "Push Token: " + pushToken;
}
}
public void OnTokenError(Exception e)
{
Debug.Log("Error asking for Push token");
Debug.Log(e.StackTrace);
}
public void OnMessageReceived(RemoteMessage remoteMessage)
{
var id = remoteMessage.MessageId;
var from = remoteMessage.From;
var to = remoteMessage.To;
var data = remoteMessage.Data;
remoteMessageText.text = $"ID: {id}\nFrom: {from}\nTo: {to}\nData: {data}";
}
public void OnNewToken(string token, Bundle bundle)
{
Debug.Log($"[HMS] Push token from OnNewToken is {token}");
if (!string.IsNullOrWhiteSpace(token))
{
pushToken = token;
tokenText.text = "Push Token: " + pushToken;
}
}
public void OnTokenError(Exception exception, Bundle bundle)
{
Debug.Log("Error asking for Push token");
Debug.Log(exception.StackTrace);
}
public void OnMessageSent(string msgId)
{
Debug.Log(msgId);
}
public void OnMessageDelivered(string msgId, Exception exception)
{
Debug.Log("Message Delivered");
Debug.Log(exception.StackTrace + " , Message ID:" + msgId);
}
public void OnSendError(string msgId, Exception exception)
{
Debug.Log(exception.StackTrace + " , Message ID:" + msgId);
}
}