Home > database >  Local notifications works for iPhone but not for iPad
Local notifications works for iPhone but not for iPad

Time:07-21

The following is just a simple test for local notifications, but while it works for iPhones (tested on iPhone 11 iOS 15.5 simulator) it doesn't work for iPads (tested on iPad 6th gen 15.5 simulator). I also tested it on similar physical devices, and the same occur in real environment.

I can't figure out a reason for why would it happen since this doesn't seem to be a UI or screen layout issue:

import SwiftUI    
struct ContentView: View {

    @State var started : Bool = false
    @State var requested : Bool = false
    @State var id : String = ""

    func setLocalNotification()
    {
        
        let content = UNMutableNotificationContent()
        content.title = "Test Local Notification"
        content.subtitle = "Hello"
        content.sound = UNNotificationSound.default

    
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: false)
        id = UUID().uuidString
      
        let request = UNNotificationRequest(identifier: id, content: content, trigger: trigger)

      
        UNUserNotificationCenter.current().add(request)

    }
        
    func removeNotification()
    {
        if (requested)
        {
            let center = UNUserNotificationCenter.current()
            center.removePendingNotificationRequests(withIdentifiers: [id])
        }
        
        requested=false;
    }
    var body: some View {
        VStack{

            Button("Cancel")
               {
                   removeNotification()
                   started=false;
               }
            Spacer()
            Button("Engage")
               {
                   if (!started)
                   {
                       UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
                           if success {
                               print("Set!")
                           } else if let error = error {
                               print(error.localizedDescription)
                           }
                       }
                       
                       requested=true;
                   
                       
                  
                       started=true;
                       
                       setLocalNotification()
                   }
                                          
               }.foregroundColor(started ? Color.brown : Color.blue)
            
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

CodePudding user response:

you are requesting for notification use success block to call notification otherwise complire run your setLocalNotification() before permission allow and nothing will happen on first launch but it will work when you run your app second time because now you are enabled notifications. So use success block to call notifications

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
             if success { 
                    // set here because if you click allow this will trigger otherwise compiler execute it before allow.
                    setLocalNotification()
             } else if let error = error {
                               print(error.localizedDescription)
             }
   }

minimize your app for showing notification also change the 60 sec to 5 sec for testing

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
  • Related