菜鸟教程小白 发表于 2022-12-11 17:58:41

ios - 在 Swift、iOS 8,9 中应用前台时推送本地通知


                                            <p><p>在IOS10中,我使用了UNMutableNotificationContent并设置了委托(delegate),</p>

<pre><code>private func pushNotification() {
    let content = UNMutableNotificationContent()
    content.title = &#34;aaa&#34;
    content.body = &#34;bbb&#34;
    content.subtitle = &#34;00000&#34;

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)

    let requestIdentifier = &#34;com.aaaaa.bbbbb&#34;

    let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)
    UNUserNotificationCenter.current().delegate = self
    UNUserNotificationCenter.current().add(request) { error in
      if error == nil {
            print(&#34;Time Interval Notification scheduled: \\\\(requestIdentifier)&#34;)
      }
    }

}
</code></pre>

<p>在前台发送通知,调用了以下函数<br/>
然后我可以通过“completionHandler”在前台显示通知</p>

<pre><code>@available(iOS 10.0, *)
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -&gt; Swift.Void) {
    completionHandler([.alert, .sound])
}
</code></pre>

<p>.<br/>
.<br/>
. </p>

<p>但在 IOS8 和 9 中,我使用了 UILocalNotification </p>

<pre><code>private func pushNotification2() {

    let notification = UILocalNotification()
    notification.alertAction = &#34;Title Message&#34;
    notification.alertBody = &#34;Body Message&#34;
    notification.fireDate = NSDate(timeIntervalSinceNow: 0) as Date
    notification.soundName = UILocalNotificationDefaultSoundName
    notification.category = &#34;INVITE_CATEGORY&#34;


    UIApplication.shared.scheduleLocalNotification(notification)

}
</code></pre>

<p>并在 AppDelegate 中找到这些</p>

<pre><code>func application(_ application: UIApplication, didReceive notification: UILocalNotification) {

}
func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, for notification: UILocalNotification, completionHandler: @escaping () -&gt; Void) {
}


func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, for notification: UILocalNotification, withResponseInfo responseInfo: , completionHandler: @escaping () -&gt; Void) {
}
</code></pre>

<p>仅当我在前台发送通知时才调用第一个函数,但第一个函数没有“completionHandler”
我应该怎么办?谢谢你的帮助</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>如本 SO <a href="https://stackoverflow.com/questions/30852870/displaying-a-stock-ios-notification-banner-when-your-app-is-open-and-in-the-fore" rel="noreferrer noopener nofollow">post</a> 中所述,在 iOS 10 之前的 iOS 中无法显示 native 通知.</p>

<p>如果您需要支持 iOS 9,则需要使用为此目的而存在的众多第三方库之一。</p>

<p> <a href="https://github.com/moritzdietsche/mdnotificationview" rel="noreferrer noopener nofollow">MDNotificationView</a>是我创建的。您可以传入一个模仿 iOS 中 native 通知外观的自定义 View 。</p>

<pre><code>// If you prefer to create your view with Interface Builder.
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: &#34;CustomView&#34;, bundle: bundle)
let views = nib.instantiate(withOwner: self, options: nil)

if 0 &lt; views.count,
    let view = views as? UIView {      

    let notificationView = MDNotificationView(view: view, position: .bottom)
    notificationView.delegate = self
    notificationView.show()
}
</code></pre>

<p>或</p>

<pre><code>// If you prefer to create your view in code.
let view = YourCustomNotificationUIView()

let notificationView = MDNotificationView(view: view, position: .bottom)
notificationView.delegate = self
notificationView.show()
</code></pre>

<p>此外,您还可以添加委托(delegate)方法:</p>

<pre><code>// MARK: Notification View Delegate

func didTap(notificationView: MDNotificationView) {
}

func notificationDidShow(notificationView: MDNotificationView) {
}

func notificationDidHide(notificationView: MDNotificationView) {
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在 Swift、iOS 8,9 中应用前台时推送本地通知,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/40371125/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/40371125/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在 Swift、iOS 8,9 中应用前台时推送本地通知