首页 > 解决方案 > 如何创建警报通知 Swift iOS?

问题描述

我想当用户从 fcm(Firebase 云消息传递)或调度程序本地通知(如警报通知)收到通知时,如下图所示:

在此处输入图像描述

这是我的代码:


func onTest() {
      
        let content = UNMutableNotificationContent()
        content.title = "Weekly Staff Meeting"
        content.body = "Every Tuesday at 2pm"
        // Configure the recurring date.
        var dateComponents = DateComponents()
        dateComponents.calendar = Calendar.current

        dateComponents.hour = 16    // 14:00 hours
        dateComponents.minute = 11
           
        // Create the trigger as a repeating event.
        let trigger = UNCalendarNotificationTrigger(
                 dateMatching: dateComponents, repeats: true)
        
        // Create the request
        let uuidString = UUID().uuidString
        let request = UNNotificationRequest(identifier: uuidString,
                    content: content, trigger: trigger)

        // Schedule the request with the system.
        let notificationCenter = UNUserNotificationCenter.current()
        notificationCenter.add(request) { (error) in
           if error != nil {
              // Handle any errors.
           }
        }
    } 

我现在的代码就像在 5 秒内消失的通常通知一样,我想让它像警报通知或 Whatsapp 呼叫通知一样。请帮忙谢谢。

标签: iosswiftpush-notificationnotificationsalarm

解决方案


由于 Apple 的限制,app dev 最多只能播放 30 秒的通知音。如果您的提示音超过 30 秒,它将播放默认通知音。

如果您的通知音在 5 秒后消失,请尝试将通知呈现选项设置为列表、徽章和声音。一旦没有通知横幅,您的 30 秒提示音将一直播放到结束。

不幸的是,没有合法的方法或任何解决方法可以让通知响铃连续响起,例如闹钟。希望我错了。

https://developer.apple.com/documentation/usernotifications/unnotificationpresentationoptions


推荐阅读