-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotification.lua
125 lines (92 loc) · 3.12 KB
/
notification.lua
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
local json = require "json"
local notifications
local deviceType = 1
local deviceToken
local requestBody = {}
local options = {}
local N = {}
N.eventDispatcher = system.newEventDispatcher()
N.get_timezone = function()
local now = os.time()
return os.difftime(now, os.time(os.date("!*t", now)))
end
N.get_tzoffset = function(timezone)
local h, m = math.modf(timezone / 3600)
return string.format("%+.4d", 100 * h + 60 * m)
end
N.networkListener = function(event)
N.eventDispatcher:dispatchEvent( { name="notify", type=event.type, data=event} )
end
N.notificationListener = function( event )
if event.type == "remoteRegistration" then
deviceToken = event.token
N.registerDevice(options.appId, options.language, options.sessions)
else
N.eventDispatcher:dispatchEvent( { name="notify", type=event.type, data=event} )
end
end
N.isError = function(appId)
local isError = false
local errorString = ""
if system.getInfo("environment") == "simulator" then
errorString = errorString.."Not supported on simulator. "
isError = true
end
if appId == nil then
errorString = errorString.."App Id cannot be nil. "
isError = true
return
end
if deviceToken == nil then
errorString = errorString.."Device token cannot be nil."
isError = true
return
end
if isError == true then
N.eventDispatcher:dispatchEvent( { name="notify", type="error", data=errorString} )
end
return isError
end
N.registerDevice = function(appId, language, sessions)
if N.isError(appId) == true then
return
end
--Time zone offset in seconds
local offsetInSeconds = (N.get_tzoffset(N.get_timezone())/100) * 3600
requestBody.app_id = appId
requestBody.device_type = deviceType
requestBody.identifier = deviceToken
requestBody.language = language
requestBody.timezone = offsetInSeconds
requestBody.game_version = system.getInfo("appVersionString")
requestBody.device_os = system.getInfo("platformVersion")
requestBody.ad_id = system.getInfo("deviceID")
requestBody.device_model = system.getInfo("model")
requestBody.session_count = sessions
local url = "https://onesignal.com/api/v1/players"
local headers = {}
headers["Content-Type"] = "application/json"
local params = {}
params.body = json.encode (requestBody)
params.headers = headers
network.request( url, "POST", N.networkListener, params )
end
N.init = function(listerner, opts)
notifications = require( "plugin.notifications.v2" )
--Add listerner
N.eventDispatcher:addEventListener("notify", listerner)
--Save options
options = opts
Runtime:addEventListener( "notification", N.notificationListener )
if system.getInfo("platform") == "ios" then
deviceType = 0
notifications.registerForPushNotifications()
else
deviceToken = notifications.getDeviceToken()
N.registerDevice(options.appId, options.language, options.sessions)
end
end
N.isDevBuild = function()
requestBody.test_type = 1
end
return N