I'm trying to use CMMotionActivityManager
's startActivityUpdatesToQueue
in the background.
But it doesn't seem to work, is there additional things that I need to do besides adding this code:
if CMMotionActivityManager.isActivityAvailable() {
CMMotionActivityManager().startActivityUpdatesToQueue(NSOperationQueue.mainQueue(), withHandler: { (activity: CMMotionActivity?) in
if (activity?.automotive)! {
let notification = UILocalNotification()
notification.alertTitle = "Arounder"
notification.alertBody = "User using car"
notification.soundName = "Notification Sound"
notification.category = "category"
UIApplication.sharedApplication().scheduleLocalNotification(notification)
print(notification.alertBody)
} else if (activity?.cycling)! {
let notification = UILocalNotification()
notification.alertTitle = "Arounder"
notification.alertBody = "User is cycling"
notification.soundName = "Notification Sound"
notification.category = "category"
UIApplication.sharedApplication().scheduleLocalNotification(notification)
print(notification.alertBody)
} else if (activity?.walking)! || (activity?.running)! {
let notification = UILocalNotification()
notification.alertTitle = "Arounder"
notification.alertBody = "User is walking/running"
notification.soundName = "Notification Sound"
notification.category = "category"
UIApplication.sharedApplication().scheduleLocalNotification(notification)
print(notification.alertBody)
} else if (activity?.stationary)! {
let notification = UILocalNotification()
notification.alertTitle = "Arounder"
notification.alertBody = "User is standing"
notification.soundName = "Notification Sound"
notification.category = "category"
UIApplication.sharedApplication().scheduleLocalNotification(notification)
print(notification.alertBody)
} else if (activity?.unknown)! {
let notification = UILocalNotification()
notification.alertTitle = "Arounder"
notification.alertBody = "Unknown activity"
notification.soundName = "Notification Sound"
notification.category = "category"
UIApplication.sharedApplication().scheduleLocalNotification(notification)
print(notification.alertBody)
}
})
}
Do I need to get permission from the user like in Core Location
(requestAlwaysAuthorization
)? Something is the .plist
file?
Thanks!