Swift 3 - 关闭徽章,警报,声音 [英] Swift 3 - Turn off badge, alert, sound

查看:172
本文介绍了Swift 3 - 关闭徽章,警报,声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里阅读了很多答案,我找不到这些问题的答案
如何在应用程序未运行时停止通知的徽章,警报和声音?
当应用程序处于后台时,如何停止通知中的徽章,警报和声音?

I've read a lot of answers here and I can't find answers of this questions How to stop badge, alert and sound from notifications when the application is not running ? How to stop badge, alert and sound from notifications when the application is in Background?

此函数:

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)

我把completionHandler([])放在没有[.badge,.alert,.sound]的情况下,当应用程序出现时,我仍然会在后台收到通知运行。

I've put completionHandler([]) without [.badge, .alert, .sound] and I still receive notifications in background when the app is runned.

此外,我的代码中还有一个函数可以在有人给你打字时发送通知,当你在应用程序中时,你看到他正在给你打字,但是当应用程序未运行时,您正在接收带有徽章警报声音的通知?如何预防?

Also I have function in my code that sends notification when somebody is typing to you and you see that he is typing to you when you are in-app, but when the application is not runned you are receiving notification with badge alert sound? How to prevent that?

PHP代码:

$body['aps'] = array(
    'content-available' => 0,
    'typingNewMessage' => 'true',
    'senderId' => $your_id
    );


推荐答案

无声 推送通知 有效负载应包含 content-available key as:

To silent push notification the payload should contain content-available key as:

{
  "aps": {
    "content-available": 1
  }
}

此外,您需要启用远程通知 背景模式 功能 设置> Xcode 项目。

Also, you need to enable Remote Notifications in Background Modes under Capabilities setting of your Xcode project.

现在你的应用程序会在收到推送通知中的一个时在后台唤醒

Now your app will wake up in the background when it receives one of these push notifications.

didReceiveRemoteNotification 收到>推送通知。

didReceiveRemoteNotification will be called in AppDelegate whenever the push notification is received.

func application(
        _ application: UIApplication,
        didReceiveRemoteNotification userInfo: [AnyHashable : Any],
        fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
    {
        let aps = userInfo["aps"] as! [String: AnyObject]
        if aps["content-available"] as? Int == 1
        {
            //Your code
        }
    }

有关如何处理前景/背景中的推送通知的更多信息,请参阅: https://www.raywenderlich.com/156966/push-notifications-tutorial-getting-started

For more on how to handle push notifications in foreground/background refer to: https://www.raywenderlich.com/156966/push-notifications-tutorial-getting-started

这篇关于Swift 3 - 关闭徽章,警报,声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆