Friday, December 15, 2023
HomeiOS Developmentswift - Native Push Notification not working in iOS

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()
}

All the pieces appears to be working effective, however the issue is the notification is just not exhibiting up. I’m attempting to study 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 relation to the pop-up a part of the notification , it is not exhibiting up. Please assist.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments