Skip to content

Commit 5069bf3

Browse files
committed
Implemented returning iOS battery level on receipt of push message
1 parent 1170179 commit 5069bf3

File tree

7 files changed

+45
-3
lines changed

7 files changed

+45
-3
lines changed

.DS_Store

-4 KB
Binary file not shown.

Swift Push 4/.DS_Store

-6 KB
Binary file not shown.

Swift Push 4/AppDelegate.swift

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2020
let versionNumber: AnyObject? = NSBundle.mainBundle().infoDictionary?["CFBundleVersion"]
2121
print ("version \(versionNumber!)")
2222

23+
UIDevice.currentDevice().batteryMonitoringEnabled = true
24+
2325
let item = NotificationData()
2426
item.alert = "Swift Push (\(versionNumber!)) starting on " + UIDevice.currentDevice().name
2527
notifications.insert(item, atIndex: 0)
@@ -60,7 +62,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
6062
let mode = receipt
6163
let versionNumber: AnyObject? = NSBundle.mainBundle().infoDictionary?["CFBundleVersion"]
6264

63-
let request = NSMutableURLRequest(URL: NSURL(string: "http://www.trease.eu/ibeacon/swiftpush/")!)
65+
let request = NSMutableURLRequest(URL: NSURL(string: "https://www.trease.eu/ibeacon/swiftpush/")!)
6466
request.HTTPMethod = "POST"
6567
var bodyData = "token=\(deviceToken.description)"
6668
bodyData += "&device=\(UIDevice.currentDevice().name)"
@@ -116,6 +118,46 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
116118
let center = NSNotificationCenter.defaultCenter()
117119
center.postNotificationName("dataChanged", object: self)
118120

121+
122+
123+
let formatter = NSNumberFormatter()
124+
formatter.numberStyle = .PercentStyle
125+
126+
let batteryLevel = formatter.stringFromNumber(UIDevice.currentDevice().batteryLevel)
127+
128+
var chargeStatus = ""
129+
130+
switch UIDevice.currentDevice().batteryState {
131+
case UIDeviceBatteryState.Unknown:
132+
chargeStatus = "Unknown"
133+
case UIDeviceBatteryState.Unplugged:
134+
chargeStatus = "Unplugged"
135+
case UIDeviceBatteryState.Charging:
136+
chargeStatus = "Charging"
137+
case UIDeviceBatteryState.Full:
138+
chargeStatus = "Full"
139+
}
140+
141+
print("battery status change: " + chargeStatus + " " + batteryLevel!)
142+
143+
let request = NSMutableURLRequest(URL: NSURL(string: "https://www.trease.eu/ibeacon/battery/")!)
144+
request.HTTPMethod = "POST"
145+
var bodyData = "&device=\(UIDevice.currentDevice().name)"
146+
bodyData += "&batterystate=" + chargeStatus
147+
bodyData += "&batterylevel=" + batteryLevel!
148+
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding)
149+
150+
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
151+
data, response, error in
152+
let x = response as? NSHTTPURLResponse
153+
print ("status code \(x?.statusCode)")
154+
}
155+
task!.resume()
156+
157+
158+
159+
160+
119161
// finished
120162
completionHandler(UIBackgroundFetchResult.NewData)
121163
}
-6 KB
Binary file not shown.

Swift Push 4/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>
22-
<string>512</string>
22+
<string>521</string>
2323
<key>LSRequiresIPhoneOS</key>
2424
<true/>
2525
<key>UIBackgroundModes</key>

Swift Push 4/Settings.bundle/Root.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<array>
77
<dict>
88
<key>DefaultValue</key>
9-
<string>512</string>
9+
<string>521</string>
1010
<key>Key</key>
1111
<string>CurrentBuildNumber</string>
1212
<key>Title</key>

0 commit comments

Comments
 (0)