How to put notes/reminder on the Lock screen - ios

I want create a app to save the notes, reminders, daily task and want to display these on the Lockscreen and when user tap the note, app will open and display the task on its screen with Customised view and background. I've done a lot of search about how to do this but got no luck.
For reference
ScreenMemo, Task Paper and many more apps doing the same.
I've no Idea which library to use to achieve this. Please help and share your code/links for better understandability.
Thanks

What you can do is to let your app display a Local Notification. Local notification pops up on the Lock screen with your desired text and clicking on it will open your App. You can as well configure which page to open the app if the notification is clicked.
Here is an example:
Step1:
import UIKit
Step2:
override func viewDidLoad() {
super.viewDidLoad()
let notification = UILocalNotification()
if #available(iOS 8.2, *) {
notification.alertTitle = "Notification Title!"
} else {
// Fallback on earlier versions
}
notification.alertBody = "This is the notification text"
notification.fireDate = NSDate(timeIntervalSinceNow: 10)
notification.applicationIconBadgeNumber = 1
notification.timeZone = NSTimeZone.defaultTimeZone()
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
//showinitial = false
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
Happy coding!

Related

How to get a push notification at a set time? (Swift 3)

I'm making an app that's supposed the user everyday at a set time about the news. It get's the text of the news through a function which calls it from an array. My question is: how do I get my app to call the function and then send me a push notification with the info text every day at, let's say, 4am?
Thanks to everyone for answering! Have a great day!
Here is some code I used before. Not a hundred-percent what you are looking for, but I hope useful for you.
You need to modify it to be sending daily
import UIKit
import UserNotifications
class ViewController: UIViewController, UNUserNotificationCenterDelegate {
var isGrantedNotificationAccess:Bool = false
#IBAction func send10SecNotification(_ sender: UIButton) {
if isGrantedNotificationAccess {
//add notification code here
//Set the content of the notification
let content = UNMutableNotificationContent()
content.title = "10 Second Notification Demo"
content.subtitle = "From MakeAppPie.com"
content.body = "Notification after 10 seconds - Your pizza is Ready!!"
//Set the trigger of the notification -- here a timer.
let trigger = UNTimeIntervalNotificationTrigger(
timeInterval: 10.0,
repeats: true)
//Set the request for the notification from the above
let request = UNNotificationRequest(
identifier: "10.second.message",
content: content,
trigger: trigger
)
//Add the notification to the currnet notification center
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
}
override func viewDidLoad() {
super.viewDidLoad()
UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound,.badge]) { (granted, error) in
self.isGrantedNotificationAccess = granted
}
}
}

iOS Local notifications - sound not playing

My local notifications are working, and the alert is showing, however the sound is not playing. I am testing on a device as I know it won't play in the simulator.
let notificationAlert = UILocalNotification()
class DailyAlarmViewController: UIViewController {
#IBOutlet weak var timePicker: UIDatePicker!
#IBAction func alarmSetButton(sender: AnyObject) {
notificationAlert.fireDate = timePicker.date
notificationAlert.timeZone = NSTimeZone.defaultTimeZone()
notificationAlert.repeatInterval = NSCalendarUnit.Day
notificationAlert.alertAction = "Open Survive to Thrive Nation!"
notificationAlert.alertBody = "Time to wake up and complete your Journal!"
notificationAlert.soundName = "Alarm_Clock.wav"
UIApplication.sharedApplication().scheduleLocalNotification(notificationAlert)
}
#IBAction func cancelAlarm(sender: AnyObject) {
UIApplication.sharedApplication().cancelLocalNotification(notificationAlert)
}
Do I need to add Background Mode or Inter App Audio? Any thoughts would be appreciated.
You must check the .wav file had add to the reaource of the app.
Can you try instead of Alarm_Clock.wav to use the default alarm sound UILocalNotificationDefaultSoundName. If it works, then may be the problem is with the Alarm_Clock.wav.

How to conditionally push local notification in ios app?

i'm working on an ios app which will send notification after the app being sent to the background. I only want the notification to work when the user set a value to true in MainviewController.swift. So I have something like this :
func setDelegateToTrue () {
Appdelegate().setToStart()
}
func setDelegateToFalse () {
Appdelegate().setToEnd()
}
and in my Appdelegate.swift, I have something like this :
var started = false
func applicationDidEnterBackground(application: UIApplication) {
if(started) {
let notification: UILocalNotification = UILocalNotification()
notification.category = "FIRST_CATEGORY"
notification.alertBody = "do not forget your app"
notification.repeatInterval = NSCalendarUnit.Day
notification.timeZone = NSTimeZone.defaultTimeZone()
UIApplication.sharedApplication().scheduleLocalNotification(notification)
started = false
}
}
func setToStart() {
started = true
}
func setToEnd() {
started = false
}
The notification works fine while there is no if statement, however, when I have the if statement and call setDelegateToTrue() in viewdidload, it stopped working. It seems like the boolean value started was to changed after calling setToStart(), but I actually can print things out of setToStart(). Can anyone help me?
Your problem is that you're creating a new instance of AppDelegate when you run AppDelegate().setToStart(). So, when the application delegate it called later it's flag is still set to false, because you set a flag on a different instance (which was immediately destroyed).
To do what you're currently trying to you need to get the delegate from UIApplication (the sharedApplication) and set the flag on that.
Bear this in mind when communication with view controllers and such in OO languages as you always need to get the instance you want to talk to rather than create a new one.
For this behaviour you can make use of NSUserDefault:
Set the value inside the Main ViewController as:
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "shouldSendNotification")
And access this user default in applicationDidEnterBackground of app delegate as follow:
func applicationDidEnterBackground(application: UIApplication) {
let shouldSendNotification = NSUserDefaults.standardUserDefaults().boolForKey("shouldSendNotification")
if shouldSendNotification {
let notification: UILocalNotification = UILocalNotification()
notification.category = "FIRST_CATEGORY"
notification.alertBody = "do not forget your app"
notification.repeatInterval = NSCalendarUnit.Day
notification.timeZone = NSTimeZone.defaultTimeZone()
UIApplication.sharedApplication().scheduleLocalNotification(notification)
NSUserDefaults.standardUserDefaults().setBool(false, forKey: "shouldSendNotification")
}
}
You need to do is
func setDelegateToTrue () {
AppDelegate.sharedAppDelegate().setToStart()
}
func setDelegateToFalse () {
AppDelegate.sharedAppDelegate().setToEnd()
}

How to get notification on iOS on change of network in not running state

In Android there is a broadcast sent by the OS whenever there is change in system configurations(like change in network state etc).
Those broadcast messages can be received by the broadcastreceiver in our app and change the behaviour of the app accordingly,though our app is not in running state.
How I can achieve the similar broadcastreceiver behavior in iOS?
You can use NSNotificationCenter to monitor changes.
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "batteryLevelChanged:",
name: NSUserDefaultsDidChangeNotification,
object: nil)
Here is the API reference:
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/
EDIT 1:
For your case, you can follow the steps and try.
Using library Reachability to get notification on changing of
network and define customized notification function.
func reachabilityChanged(note: NSNotification) {
let reachability = note.object as! Reachability
if reachability.isReachable() {
if reachability.isReachableViaWiFi() {
print("Reachable via WiFi")
} else {
print("Reachable via Cellular")
}
} else {
print("Network not reachable")
}
}
In your notification function, create a corresponding local
notification
var notification = UILocalNotification()
notification.alertBody = "Notification message!!!" // text that will be displayed in the notification
notification.alertAction = "open" // text that is displayed after "slide to..." on the lock screen - defaults to "slide to view"
notification.fireDate = NSDate() // todo item due date (when notification will be fired)
notification.soundName = UILocalNotificationDefaultSoundName // play default sound
notification.userInfo = ["UUID": UUID, ] // assign a unique identifier to the notification so that we can retrieve it later
notification.category = "CATEGORY"
UIApplication.sharedApplication().scheduleLocalNotification(notification)
You have use Reachability class in order to implement network change notification, here is example:
https://developer.apple.com/library/ios/samplecode/Reachability/Listings/Reachability_Reachability_h.html

How to put notifications on iOS application to repeat every one (1) hour?

I tried to put notifications in my app, it was supposed to repeat every one hour but it repeat unregulated, to be clear, it repeats sometimes 30min sometimes one hour sometimes for a long time etc..
Code that I used in "AppDelegate.swift":
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
//Notification Repeat
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, categories: nil))
return true
}
and code that I used in "ViewController.swift":
//Notification Repeat
var Time = 1
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//Notification Repeat
var Timer = NSTimer.scheduledTimerWithTimeInterval(3600.0, target: self, selector: Selector("activateNotifications"), userInfo: nil, repeats: true)
}
//Notification Repeat
func activateNotifications() {
Time -= 1
if (Time <= 0){
var activateNotifications = UILocalNotification()
activateNotifications.alertAction = “Hey"
activateNotifications.alertBody = “Hello World!"
activateNotifications.fireDate = NSDate(timeIntervalSinceNow: 0)
UIApplication.sharedApplication().scheduleLocalNotification(activateNotifications)
}
}
Can someone help me, where I made mistake ?
You don't need the timer at all. The UILocalNotification class has a property entitled repeatInterval that, as you can expect, set the interval at which the notification will be repeated.
According to this, you can schedule a local notifications that is repeated every hour in the following way:
func viewDidLoad() {
super.viewDidLoad()
var notification = UILocalNotification()
notification.alertBody = "..." // text that will be displayed in the notification
notification.fireDate = NSDate() // right now (when notification will be fired)
notification.soundName = UILocalNotificationDefaultSoundName // play default sound
notification.repeatInterval = NSCalendarUnit.CalendarUnitHour // this line defines the interval at which the notification will be repeated
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
NOTE: Be sure that you execute the code when you launch the notification only once, since it schedules a different notification every time that it is executed. For a better understanding of local notifications, you can read Local Notifications in iOS 8 with Swift (Part 1) and Local Notifications in iOS 8 with Swift (Part 2).

Resources