0

I'm fairly new to programming and working on an app that will notify the users when my store is open. I'm using firebase to store data and i'm looking to use local notifications rather than push.

I have my app delegate set up according to some online sources to send notifications, but i'm not sure how to call a function for it. I'm using swift 4 in Xcode.

import Firebase

var refNotificationValue = DatabaseReference!

@IBAction func openButtonClicked(_ sender: UIButton) {
    // Alert Controller confirming actions
   self.shouldSendNotification()
}

// DidLoad()

refNotificationValue = database.database().reference().child("Notifications").child("enabled")     // set to yes

// EndLoad()

func shouldSendNotification () {
    refNotificationValue?.observeSingleEvent(of: .value, with: { (snapshot) in
        let dict = snapshot.value as! [String: Any]
        let status = dict["enabled"] as! String

if status == "yes" {
      setToOpen()
      sendNotification()
} else {
     setToOpen()
   }
 })
}

func sendNotification() {
      ????????
}

Thanks so much in advance!!

Tim Mowod
  • 3
  • 1
  • 3

1 Answers1

0

Assuming you are observing your open and closed times from your database, there are 3 ways to go about it.

1.) Create node server to do the observing for you and push the notifications. This approach sounds complex but its the pretty simple and the approach I went with. firebase has great documentation on it and you can find great tutorials like this one.

2.) Use a background fetch in your app delegate like in this post. This only happens periodically, so it may not be an exact fit.

3.) Use firebase cloud functions to do the job of the node server. Check out this post to see how to do it.

You cannot maintain database observe functions in the background as discussed here.

Dustin Spengler
  • 5,478
  • 4
  • 28
  • 36