0

I am trying to set push notifications in my app.

I have enabled my APN certificates.. enter image description here

And I have turned push notifications on in Xcode.. enter image description here

I have turned push notifications on in previous xcode projects and it worked fine. This is the code I have..

 //add push notifications every 5 hours
func pushNotifications() {
    let pushNotification = UNMutableNotificationContent()
    pushNotification.title = "Time to track your water useage!"
    pushNotification.badge = 1

    let minute:TimeInterval = 60.0
    let hour:TimeInterval = 60.0 * minute
    let eighthDay:TimeInterval = 5 * hour

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: eighthDay, repeats: true)
    let request = UNNotificationRequest(identifier: "timerDone", content: pushNotification, trigger: trigger)
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}

I am calling this in my viewdidload func

What am I doing wrong?

Cameron
  • 185
  • 2
  • 11
  • 1
    `UNMutableNotificationContent` creates a **local** notification. That's not a push notification. – vadian Aug 22 '18 at 15:52
  • @vadian oh. why aren't my local notifications working then? – Cameron Aug 22 '18 at 15:54
  • 1
    you need to implement willPresent and return the needed types , as notifications don't show default when app is in foreground , also make sure that you requested the permissions , look here http://www.thomashanning.com/push-notifications-local-notifications-tutorial/ – Shehata Gamal Aug 22 '18 at 15:54
  • thank you @Sh_Khan I will check this out – Cameron Aug 22 '18 at 16:01
  • 1
    Possible duplicate of [How can I create local notifications in iOS?](https://stackoverflow.com/questions/6047117/how-can-i-create-local-notifications-in-ios) – Rahim Khalid Aug 22 '18 at 16:12

1 Answers1

0

You can try this code,

func sendLocalPush() {
    let objNotificationContent = UNMutableNotificationContent()
    objNotificationContent.title = "Manish Kumar"
    objNotificationContent.body = "iOS Developer"
    objNotificationContent.userInfo = getUserInfoDict()
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1.0, repeats: false)
    let request = UNNotificationRequest(identifier: "LocalNotification", content: objNotificationContent, trigger: trigger)
    /// 3. schedule localNotification
    let center = UNUserNotificationCenter.current()
    center.add(request, withCompletionHandler: {(_ error: Error?) -> Void in
    })
}

Hope this helps.

manishsharma93
  • 1,039
  • 1
  • 11
  • 26