-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathNotificationManager.as
More file actions
292 lines (258 loc) · 11 KB
/
NotificationManager.as
File metadata and controls
292 lines (258 loc) · 11 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package notification
{
import com.mteamapp.StringFunctions;
import flash.events.EventDispatcher;
import flash.utils.clearTimeout;
import flash.utils.setTimeout;
/**Notification tocken ready*/
[Event(name="TOKEN_REGISTER_COMPELETED", type="notification.NotificationEvent")]
/**Notification receved*/
[Event(name="NOTIFICATION", type="notification.NotificationEvent")]
[Event(name="FOREGROUND_NOTIFICATION", type="notification.NotificationEvent")]
public class NotificationManager extends EventDispatcher
{
public static var ONESIGNAL_APP_ID:String;
public static var GCM_PROJECT_NUMBER:String;
public static var Notification_Event:NotificationManager
public static var token:String ;
private var _timeOutId:uint
private static var autoAlertBox:Boolean;
/**com.milkmangames.nativeextensions.events.PNOSEvent*/
private static var PNOSEventClass:Class ;
/**com.milkmangames.nativeextensions.EasyPush*/
private static var EasyPushClass:Class ;
/**Dont call this function here !! use setup!!!*/
public function NotificationManager(ONESIGNAL_APP_ID_p:String='',GCM_PROJECT_NUMBER_p:String='',autoAlerOnNativeBox:Boolean=true)
{
super();
autoAlertBox = autoAlerOnNativeBox ;
trace("SetUp easy push");
ONESIGNAL_APP_ID = ONESIGNAL_APP_ID_p ;
GCM_PROJECT_NUMBER = GCM_PROJECT_NUMBER_p ;
if(PNOSEventClass==null)
{
PNOSEventClass = Obj.generateClass("com.milkmangames.nativeextensions.events.PNOSEvent") ;
}
if(EasyPushClass==null)
{
EasyPushClass = Obj.generateClass("com.milkmangames.nativeextensions.EasyPush") ;
}
if(ONESIGNAL_APP_ID_p!='' && GCM_PROJECT_NUMBER_p!='')
{
EasyPushExample();
}
}
/**This will returns an instance on NofificatnionManager to cathc its events<br>
* there is no need to call this*/
public static function setup(ONESIGNAL_APP_ID_p:String='',GCM_PROJECT_NUMBER_p:String='',autoAlerOnNativeBox:Boolean=true):NotificationManager
{
autoAlertBox = autoAlerOnNativeBox ;
trace("SetUp easy push");
Notification_Event = new NotificationManager(ONESIGNAL_APP_ID_p,GCM_PROJECT_NUMBER_p,autoAlerOnNativeBox)
ONESIGNAL_APP_ID = ONESIGNAL_APP_ID_p
GCM_PROJECT_NUMBER = GCM_PROJECT_NUMBER_p
NotificationManager.Notification_Event.EasyPushExample();
return Notification_Event ;
}
private function EasyPushExample()
{
//Controll permissions↓
var currentPermissions:String = StringFunctions.clearSpacesAndTabs(DevicePrefrence.appDescriptor) ;
var requiredPermissionIos:String = "<key>application-identifier</key>\n" +
"\t<string>??????????."+DevicePrefrence.appID+"</string>\n" +
"<key>aps-environment</key>\n" +
"\t<string>development</string><!--\"development\" for adhoc test, \"production\" for Appstore release-->\n" +
"<key>get-task-allow</key> <true/> <!--Remove this line for Appstore release-->\n" +
"<key>keychain-access-groups</key>\n" +
"\t<array>\n" +
"\t\t<string>??????????."+DevicePrefrence.appID+"</string> <!--Add team id-->\n" +
"\t</array>";
if(DevicePrefrence.isItPC && currentPermissions.indexOf("<key>application-identifier</key>")==-1)
{
throw "You have to add below permission on <iPhone><Entitlements> <![CDATA[ \n\n\n"+requiredPermissionIos+'\n\n]]>\n\n' ;
}
//control android permission : <android> <manifestAdditions><![CDATA[
var neceraryLines:String = '•' ;
var targetSDK:uint = 22 ;
var sdkTargetString:String = 'android:targetSdkVersion="' ;
var minSDK:uint = 9 ;
var sdkMinTargetString:String = 'android:minSdkVersion="' ;
var indexOf:int ;
if((indexOf = currentPermissions.indexOf(sdkMinTargetString))!=-1)
{
minSDK = uint(currentPermissions.substring(indexOf+sdkMinTargetString.length,currentPermissions.indexOf('"',indexOf+sdkMinTargetString.length)));
}
if((indexOf = currentPermissions.indexOf(sdkTargetString))!=-1)
{
targetSDK = uint(currentPermissions.substring(indexOf+sdkTargetString.length,currentPermissions.indexOf('"',indexOf+sdkTargetString.length)));
}
var androidPermission:String = neceraryLines+'<manifest android:installLocation="auto">\n' +
'\t<uses-sdk android:minSdkVersion="'+minSDK+'" android:targetSdkVersion="'+targetSDK+'" />\n' +
'\t<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>\n' +
'\t<uses-permission android:name="android.permission.READ_PHONE_STATE"/>\n' +
'\t<uses-permission android:name="android.permission.INTERNET"/>\n' +
'\t<uses-permission android:name="android.permission.GET_ACCOUNTS"/>\n' +
'\t<uses-permission android:name="android.permission.GET_TASKS"/>\n' +
'\t<uses-permission android:name="android.permission.WAKE_LOCK"/>\n' +
'\t<uses-permission android:name="android.permission.VIBRATE"/>\n' +
'\t<permission android:name="air.'+DevicePrefrence.appID+'.permission.C2D_MESSAGE" android:protectionLevel="signature" />\n' +
'\t<uses-permission android:name="air.'+DevicePrefrence.appID+'.permission.C2D_MESSAGE" />\n' +
'\t<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />\n' +
neceraryLines+'\t<application>\n' +
neceraryLines+'\t\t<receiver android:name="com.milkmangames.extensions.android.push.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" >\n' +
neceraryLines+'\t\t\t<intent-filter>\n' +
'\t\t\t\t<action android:name="com.google.android.c2dm.intent.RECEIVE" />\n' +
'\t\t\t\t<action android:name="com.google.android.c2dm.intent.REGISTRATION" />\n' +
'\t\t\t\t<category android:name="air.'+DevicePrefrence.appID+'" />\n' +
neceraryLines+'\t\t\t</intent-filter>\n' +
neceraryLines+'\t\t</receiver>\n' +
'\t\t<service android:name="com.milkmangames.extensions.android.push.GCMIntentService" />\n' +
neceraryLines+'\t</application>\n' +
neceraryLines+'</manifest>' ;
var allAndroidPermission:Array = androidPermission.split('\n');
var leftPermission:String = '' ;
var androidManifestMustUpdate:Boolean = false ;
for(var i:int = 0 ; i<allAndroidPermission.length ; i++)
{
var isNessesaryToShow:Boolean = isNessesaryLine(allAndroidPermission[i]) ;
if(currentPermissions.indexOf(StringFunctions.clearSpacesAndTabs(removeNecessaryBoolet(allAndroidPermission[i])))==-1)
{
trace("permission not found : "+allAndroidPermission[i]);
androidManifestMustUpdate = true ;
leftPermission += removeNecessaryBoolet(allAndroidPermission[i])+'\n' ;
}
else if(isNessesaryToShow)
{
leftPermission += removeNecessaryBoolet(allAndroidPermission[i])+'\n' ;
}
else
{
//leftPermission += '-'+allAndroidPermission[i]+'\n' ;
}
}
function isNessesaryLine(line:String):Boolean
{
return line.indexOf(neceraryLines)!=-1 ;
}
function removeNecessaryBoolet(line:String):String
{
return line.split(neceraryLines).join('') ;
}
if(DevicePrefrence.isItPC && androidManifestMustUpdate)
{
//throw "Add bellow permission to <android> <manifestAdditions><![CDATA[\n\n\n"+leftPermission+"\n\n]]>\n\n";
}
//Controll permissions↑
if (EasyPushClass==null || !(EasyPushClass as Object).isSupported())
{
log("EasyPush is not supported on this platform (not android or ios!)");
return;
}
if (!(EasyPushClass as Object).areNotificationsAvailable())
{
log("Notifications are not available!");
return;
}
log("Easy push created")
if (!validateConstants()) return;
setupOneSignal();
}
private function setupOneSignal():void
{
// onesignal mode
trace('ONESIGNAL_APP_ID :',ONESIGNAL_APP_ID)
trace('GCM_PROJECT_NUMBER :',GCM_PROJECT_NUMBER)
log("init OneSignal...");
try
{
(EasyPushClass as Object).initOneSignal(ONESIGNAL_APP_ID, GCM_PROJECT_NUMBER, autoAlertBox);
}catch(e)
{
trace("Esy push >>>> "+e);
}
log("did init OneSignal.");
(EasyPushClass as Object).oneSignal.addEventListener((PNOSEventClass as Object).ALERT_DISMISSED,onAlertDismissed);
(EasyPushClass as Object).oneSignal.addEventListener((PNOSEventClass as Object).FOREGROUND_NOTIFICATION,onForegroundNotification);
(EasyPushClass as Object).oneSignal.addEventListener((PNOSEventClass as Object).RESUMED_FROM_NOTIFICATION,onNotification);
(EasyPushClass as Object).oneSignal.addEventListener((PNOSEventClass as Object).TOKEN_REGISTERED,onTokenRegistered);
(EasyPushClass as Object).oneSignal.addEventListener((PNOSEventClass as Object).TOKEN_REGISTRATION_FAILED,onRegFailed);
tryeToConnectNotificationRegister()
}
private function validateConstants():Boolean
{
if (ONESIGNAL_APP_ID=='')
{
log("You did not put your onesignal id in EasyPushExample.as.");
return false;
}
if (GCM_PROJECT_NUMBER=='')
{
log("WARNING: won't work on android until id set in EasyPushExample.as.");
return true;
}
return true;
}
/////////////////////event
//com.milkmangames.nativeextensions.events.PNEvent
private function onTokenRegistered(e:*):void
{
log("token registered:"+e.token);
token = e.token;
this.dispatchEvent(new NotificationEvent (NotificationEvent.TOKEN_REGISTER_COMPELETED,pnEvent(e)))
clearTimeout(_timeOutId)
}
//com.milkmangames.nativeextensions.events.PNEvent
private function onRegFailed(e:*):void
{
log("reg failed: "+e.errorId+"="+e.errorMsg);
loop()
}
private function loop():void
{
clearTimeout(_timeOutId)
_timeOutId = setTimeout(setupOneSignal,5000)
}
//com.milkmangames.nativeextensions.events.PNEvent
private function onAlertDismissed(e:*):void
{
log("dismissed alert "+e.alert);
loop()
}
//com.milkmangames.nativeextensions.events.PNEvent
private function onNotification(e:*):void
{
log(e.type+"="+e.rawPayload+","+e.badgeValue+","+e.title+" customPayload : "+e.customPayload+" : "+JSON.stringify(e.customPayload,null,' '));
this.dispatchEvent(new NotificationEvent(NotificationEvent.NOTIFICATION,pnEvent(e),true,false,e.customPayload))
}
//com.milkmangames.nativeextensions.events.PNEvent
private function onForegroundNotification(e:*):void
{
log(e.type+"="+e.rawPayload+","+e.badgeValue+","+e.title+" customPayload : "+e.customPayload+" : "+JSON.stringify(e.customPayload,null,' '));
this.dispatchEvent(new NotificationEvent(NotificationEvent.FOREGROUND_NOTIFICATION,pnEvent(e),true,false,e.customPayload))
}
///////////////////end event
private function log(msg:String):void
{
trace("[Push Notificatoni]"+msg);
}
//com.milkmangames.nativeextensions.events.PNEvent
private function pnEvent(e:*):PNEventManager
{
var _pnEvnet:PNEventManager = new PNEventManager()
_pnEvnet.alert = e.alert
_pnEvnet.badgeValue = e.badgeValue
_pnEvnet.errorId = e.errorId
_pnEvnet.errorMsg = e.errorMsg
_pnEvnet.rawPayload = e.rawPayload
_pnEvnet.customPayload = e.customPayload
_pnEvnet.title = e.title
_pnEvnet.token = e.token
_pnEvnet.type = e.type
return _pnEvnet
}
private function tryeToConnectNotificationRegister():void
{
this.dispatchEvent(new NotificationEvent(NotificationEvent.TOKEN_REGISTER_START))
}
}
}