7 C
London
Friday, December 15, 2023

swift – Native 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 occasion = NotificationManager() // Singleton
    
    func requestAuthorization() {
        
        let choices : UNAuthorizationOptions = [.alert, .sound, .badge]
        
        UNUserNotificationCenter.present().requestAuthorization(choices: choices) { (success, error) in
            if let error = error{
                print("ERROR : (error)")
            }
            else {
                print("Success!")
            }
        }
    }
    
    func scheduleNotification() {
        
        let content material = UNMutableNotificationContent()
        content material.title = "That is my first push notification"
        content material.subtitle = "This was sooooo a lot enjoyable!"
        content material.sound = .default
        content material.badge = 1
        
        //time
        let set off = UNTimeIntervalNotificationTrigger(timeInterval: 1.0, repeats: false)
        //calender
        //location
        
        let request = UNNotificationRequest(identifier: UUID().uuidString, content material: content material, set off: set off)
        UNUserNotificationCenter.present().add(request)
    }
}

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

#Preview {
    LocalNotifications()
}

Every little thing appears to be working nice, however the issue is the notification shouldn’t be exhibiting up. I’m making an attempt to be taught push notification in iOS from a youtube video, issues appears to be working good for the youtuber, however I additionally did the identical factor and it is not woking for me.

I attempted to entry the permission of the consumer, which is a Success. However in the case of the pop-up a part of the notification , it is not exhibiting up. Please assist.

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here