/
Push2App.cs
266 lines (252 loc) · 11.1 KB
/
Push2App.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using UnityEngine;
using KiiCorp.Cloud.Storage;
using KiiCorp.Cloud.Unity;
using System;
using System.Collections;
public class Push2App : MonoBehaviour
{
private string payload = "";
private string message = "--- Logs will show here ---";
private KiiPushPlugin kiiPushPlugin = null;
private static string USER_NAME = "unitypushdemo" + Environment.TickCount;
private const string PASSWORD = "password";
private static string BUCKET_NAME = "userbucket";
private KiiPushPlugin.KiiPushMessageReceivedCallback receivedCallback;
void Awake ()
{
}
// Use this for initialization
void Start ()
{
Debug.Log ("#####Main.Start");
this.kiiPushPlugin = GameObject.Find ("KiiPushPlugin").GetComponent<KiiPushPlugin> ();
String pushMessage = this.kiiPushPlugin.GetLastMessage ();
if (pushMessage != null) {
this.message = pushMessage;
return;
}
this.receivedCallback = (ReceivedMessage message) => {
switch (message.PushMessageType) {
case ReceivedMessage.MessageType.PUSH_TO_APP:
Debug.Log ("#####PUSH_TO_APP Message");
this.OnPushNotificationsReceived (message);
break;
case ReceivedMessage.MessageType.PUSH_TO_USER:
Debug.Log ("#####PUSH_TO_USER Message");
this.OnPushNotificationsReceived (message);
break;
case ReceivedMessage.MessageType.DIRECT_PUSH:
Debug.Log ("#####DIRECT_PUSH Message");
this.OnPushNotificationsReceived (message);
break;
}
};
this.kiiPushPlugin.OnPushMessageReceived += this.receivedCallback;
if (KiiUser.CurrentUser != null) {
Invoke ("registerPush", 0);
return;
}
Debug.Log ("##### USER = " + USER_NAME);
KiiUser.LogIn (USER_NAME, PASSWORD, (KiiUser u1, Exception e1) => {
if (e1 != null) {
KiiUser newUser = KiiUser.BuilderWithName (USER_NAME).Build ();
Debug.Log ("#####Register");
newUser.Register (PASSWORD, (KiiUser u2, Exception e2) => {
Debug.Log ("#####callback Register");
if (e2 != null) {
Debug.Log ("#####failed to Register");
this.ShowException ("Failed to register user.", e2);
return;
} else {
Invoke ("registerPush", 0);
}
});
} else {
Invoke ("registerPush", 0);
}
});
}
void OnApplicationPause (bool pauseStatus)
{
if (!pauseStatus) {
string lastMessage = this.kiiPushPlugin.GetLastMessage ();
if (lastMessage != null) {
this.message += "#### launch from notification!!!! " + lastMessage;
return;
}
}
}
void registerPush ()
{
#if UNITY_IPHONE
KiiPushInstallation.DeviceType deviceType = KiiPushInstallation.DeviceType.IOS;
#elif UNITY_ANDROID
KiiPushInstallation.DeviceType deviceType = KiiPushInstallation.DeviceType.ANDROID;
#else
KiiPushInstallation.DeviceType deviceType = KiiPushInstallation.DeviceType.ANDROID;
#endif
if (this.kiiPushPlugin == null) {
Debug.Log ("#####failed to find KiiPushPlugin");
return;
}
this.kiiPushPlugin.RegisterPush ((string pushToken, Exception e0) => {
if (e0 != null) {
Debug.Log ("#####failed to RegisterPush");
this.message = "#####failed to RegisterPush : " + pushToken;
return;
}
Debug.Log ("#####RegistrationId=" + pushToken);
this.message = "Token : " + pushToken + "\n";
Debug.Log ("#####Install");
KiiUser.PushInstallation (true).Install (pushToken, deviceType, (Exception e3) => {
if (e3 != null) {
Debug.Log ("#####failed to Install");
this.ShowException ("Failed to install PushNotification -- pushToken=" + pushToken, e3);
return;
}
KiiBucket bucket = KiiUser.CurrentUser.Bucket (BUCKET_NAME);
// bucket.Acl(BucketAction.CREATE_OBJECTS_IN_BUCKET).Subject(KiiAnyAuthenticatedUser.Get()).Save(ACLOperation.GRANT, (KiiACLEntry<KiiBucket, BucketAction> entry, Exception e5)=>{
// if (e5 != null)
// {
// Debug.Log ("#####Failed to grant acl to the bucket");
// this.ShowException("Failed to grant acl to the bucket", e5);
// return;
// }
Debug.Log ("#####Subscribe");
KiiUser.CurrentUser.PushSubscription.Subscribe (bucket, (KiiSubscribable subscribable, Exception e6) => {
Debug.Log ("#####callback Subscribe");
if (e6 != null) {
if (e6 is ConflictException) {
this.message += "Bucket is already subscribed" + "\n";
this.message += "Push is ready";
Debug.Log ("#####all setup success!!!!!!");
return;
}
Debug.Log ("#####failed to Subscribe");
this.ShowException ("Failed to subscribe bucket", e6);
return;
} else {
this.message += "Push is ready";
Debug.Log ("#####all setup success!!!!!!");
}
});
// });
});
});
}
void OnGUI ()
{
ScalableGUI gui = new ScalableGUI ();
gui.Label (5, 5, 310, 20, "Push2App scene");
if (gui.Button (200, 5, 120, 35, "-> Push2User")) {
this.kiiPushPlugin.OnPushMessageReceived -= this.receivedCallback;
Application.LoadLevel ("push2user");
}
this.payload = gui.TextField (0, 45, 320, 50, this.payload);
if (gui.Button (0, 100, 160, 50, "Create Object")) {
KiiBucket bucket = KiiUser.CurrentUser.Bucket (BUCKET_NAME);
KiiObject obj = bucket.NewKiiObject ();
obj ["payload"] = this.payload;
obj.Save ((KiiObject o, Exception e) => {
if (e != null) {
Debug.Log ("#####" + e.Message);
Debug.Log ("#####" + e.StackTrace);
this.ShowException ("Failed to save object", e);
return;
}
this.message = "#####creating object is successful!!";
});
}
if (gui.Button (160, 100, 160, 50, "Clear Log")) {
this.message = "--- Logs will show here ---";
}
if (gui.Button (0, 150, 160, 50, "Register Push")) {
Invoke ("registerPush", 0);
}
if (gui.Button (160, 150, 160, 50, "Unregister Push")) {
this.kiiPushPlugin.UnregisterPush ((Exception e) => {
if (e != null) {
Debug.Log ("#####" + e.Message);
Debug.Log ("#####" + e.StackTrace);
this.ShowException ("#####Unregister push is failed!!", e);
return;
}
this.message = "#####Unregister push is successful!!";
});
}
if (gui.Button (0, 200, 160, 50, "Subscribe bucket")) {
KiiUser user = KiiUser.CurrentUser;
KiiBucket bucket = user.Bucket (BUCKET_NAME);
KiiPushSubscription subscription = user.PushSubscription;
subscription.Subscribe (bucket, (KiiSubscribable target, Exception e) => {
if (e != null) {
if (e is ConflictException) {
this.message += "\n" + "Bucket is already subscribed";
return;
}
Debug.Log ("#####" + e.Message);
Debug.Log ("#####" + e.StackTrace);
this.ShowException ("#####Subscribe is failed!!", e);
return;
}
this.message = "#####Subscribe is successful!!";
});
}
if (gui.Button (160, 200, 160, 50, "Unsubscribe bucket")) {
KiiUser user = KiiUser.CurrentUser;
KiiBucket bucket = user.Bucket (BUCKET_NAME);
KiiPushSubscription subscription = user.PushSubscription;
subscription.Unsubscribe (bucket, (KiiSubscribable target, Exception e) => {
if (e != null) {
Debug.Log ("#####" + e.Message);
Debug.Log ("#####" + e.StackTrace);
this.ShowException ("#####Unsubscribe is failed!!", e);
return;
}
this.message = "#####Unsubscribe is successful!!";
});
}
if (gui.Button (0, 250, 160, 50, "Check subscription")) {
KiiUser user = KiiUser.CurrentUser;
KiiBucket bucket = user.Bucket (BUCKET_NAME);
KiiPushSubscription subscription = user.PushSubscription;
subscription.IsSubscribed (bucket, (KiiSubscribable target, bool isSubscribed, Exception e) => {
if (e != null) {
Debug.Log ("#####" + e.Message);
Debug.Log ("#####" + e.StackTrace);
this.ShowException ("#####Check subscription is failed!!", e);
return;
}
this.message = "#####Subscription status : " + isSubscribed;
});
}
gui.TextArea (5, 310, 310, 170, this.message, 10);
}
private void ShowException (string msg, Exception e)
{
Debug.Log ("#####" + e.Message);
Debug.Log ("#####" + e.StackTrace);
this.message = "#####ERROR: " + msg + " type=" + e.GetType () + "\n";
if (e.InnerException != null) {
this.message += "#####InnerExcepton=" + e.InnerException.GetType () + "\n";
this.message += "#####InnerExcepton.Message=" + e.InnerException.Message + "\n";
this.message += "#####InnerExcepton.Stacktrace=" + e.InnerException.StackTrace + "\n";
}
this.message += "#####" + e.Message + "\n" + "#####" + e.StackTrace;
}
void Update ()
{
}
public void OnPushNotificationsReceived (ReceivedMessage message)
{
this.message = "#####PushNotification Received" + "\n";
this.message += "#####Type=" + message.PushMessageType + "\n";
this.message += "#####Sender=" + message.Sender + "\n";
this.message += "#####Scope=" + message.ObjectScope + "\n";
this.message += "#####msg=" + message.GetString ("msg") + "\n";
PushToAppMessage msg = (PushToAppMessage)message;
msg.KiiObject.Refresh ((KiiObject obj, Exception e) => {
this.message += "#####payload=" + obj.GetString ("payload");
});
}
}