swift – Local Push Notification not working in iOS


//
//  LocalNotifications.swift
//  PushNotifications_Tutorial
//
//  Created by Abhishek Gautam on 15/12/23.
//

import SwiftUI
import UserNotifications

class NotificationManager {
    
    static let instance = NotificationManager() // Singleton
    
    func requestAuthorization() {
        
        let options : UNAuthorizationOptions = [.alert, .sound, .badge]
        
        UNUserNotificationCenter.current().requestAuthorization(options: options) { (success, error) in
            if let error = error{
                print("ERROR : \(error)")
            }
            else {
                print("Success!")
            }
        }
    }
    
    func scheduleNotification() {
        
        let content = UNMutableNotificationContent()
        content.title = "This is my first push notification"
        content.subtitle = "This was sooooo much fun!"
        content.sound = .default
        content.badge = 1
        
        //time
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1.0, repeats: false)
        //calender
        //location
        
        let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(request)
    }
}

struct LocalNotifications: View {
    var body: some View {
        VStack(spacing :40){
            Button("Request Permission") {
                NotificationManager.instance.requestAuthorization()
                //print("Button Tapped!")
            }
            Button("Schedule Notification") {
                NotificationManager.instance.scheduleNotification()
            }
        }
    }
}

#Preview {
    LocalNotifications()
}

Everything seems to be working fine, but the problem is the notification is not showing up. I am trying to learn push notification in iOS from a youtube video, things seems to be working good for the youtuber, but I also did the same thing and it’s not woking for me.

I tried to access the permission of the user, which is a Success. But when it comes to the pop-up part of the notification , it isn’t showing up. Please help.

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img