Je souhaite créer une application de gestion des présences pour iOS. L'application aura un bouton de démarrage, puis si elle tapait, l'application aurait une notification de bannière pour vous accueillir.
Le problème est que je ne peux pas avoir de notification de bannière lorsque l'application est au premier plan. Je vais montrer mon code ci-dessous. Si je veux recevoir une notification, je dois retourner à l'écran d'accueil ou exécuter d'autres applications à la place. Sur mon application, le bouton Set Notification
permet de recevoir des notifications et le bouton Start
sert à définir la notification. J'aurais une notification immédiatement, mais pour le moment, je dois rentrer à la maison, alors j'ai réglé l' timeInterval: 5
.
ContentView.swift
import SwiftUI import Foundation import UserNotifications struct ContentView: View { var body: some View { VStack { Button(action: { self.setNotification() }) { Text("Set Notification") }.padding() //added here Button(action: { self.btnSend() }) { Text("Start") }.padding() } } func setNotification() -> Void { UNUserNotificationCenter.current().requestAuthorization( options: [.alert,.sound,.badge], completionHandler: {didAllow,Error in print(didAllow) // }) } func btnSend() { print("btnSend") let content = UNMutableNotificationContent() content.title = NSString.localizedUserNotificationString(forKey: "Hello!", arguments: nil) content.body = NSString.localizedUserNotificationString(forKey: "Good morning hehe", arguments: nil) content.sound = UNNotificationSound.default // Deliver the notification in five seconds. let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false) let request = UNNotificationRequest(identifier: "FiveSecond", content: content, trigger: trigger) // Schedule the notification. let center = UNUserNotificationCenter.current() center.add(request) { (error : Error?) in if let theError = error { // Handle any errors } } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
Quelqu'un a-t-il de belles solutions?
3 Réponses :
La bannière de notification ne montre pas si l'application est au premier plan, vous devez capturer la notificafion dans AppDelegate
, créer votre propre vue personnalisée et la gérer à partir de là.
Peut-être avez-vous juste besoin d'ajouter quelques codes dans AppDelegate
.
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { // add UNUserNotificationCenterDelegate func userNotificationCenter( _ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { completionHandler([.alert, .badge, .sound]) } // add this function func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. UNUserNotificationCenter.current().delegate = self // add this return true } // ... omitted }
Solution parfaite.
Utilisez le
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { completionHandler([.alert, .sound]) }
méthode pour la capture de notification au premier plan et afficher une alerte, un badge et ajouter du son.
Est-ce que cela répond à votre question? Recevez une notification push lorsque l'application est au premier plan iOS