简体   繁体   English

iOS:是否可以在没有声音的情况下向通知中心发送推送?

[英]iOS: Is it possible to send a push to Notification Center without a sound?

On Android, I can send a silent push that gets delivered to the system tray.在 Android 上,我可以发送一个静默推送,然后发送到系统托盘。 On iOS, I'm not sure if this is possible.在 iOS 上,我不确定这是否可行。 I removed the alert property from aps , and I'm sending an empty string for sound .我从aps删除了alert属性,并为sound发送了一个空字符串。 What I'm seeing is that the badge count is updated silently (which is good), but there is nothing in Notification Center.我看到的是,徽章计数是静默更新的(这很好),但通知中心没有任何内容。 So is there a way to send a push to NC without a sound?那么有没有办法在没有声音的情况下向 NC 发送推送?

Note that I do not want the push to pop up on the screen (ie the user should not see it unless they swipe down on the screen to reveal what's been delivered. In other words, I'm trying to match the behavior on Android).请注意,我不希望推送在屏幕上弹出(即用户不应该看到它,除非他们在屏幕上向下滑动以显示已交付的内容。换句话说,我正在尝试匹配 Android 上的行为) .

yes, apple support it, the push data format is是的,苹果支持,推送数据格式是

{
    "aps":{
        "aa":"123",
        "content-available":1
    }
}

important key is this关键是这个
"content-available":1 “内容可用”:1

If you don't want sound then don't send sound param in payload.如果您不想要声音,则不要在有效载荷中发送sound参数。

{
    "aps" : {
        "alert" : "You got your emails.",
        "badge" : 1
    },
    "acme1" : "test"
}

also you can remove .alert and .sound from willPresent.. method of push in appdelegate.swift file您也可以从willPresent..删除.alert.sound willPresent.. appdelegate.swift文件中的推送方法

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
    completionHandler([.badge]) //remove .sound, .alert
}

The only way to achieve what you want to do will be to specify "provisional" when asking the notification permission on the device, which is supported on iOS >= 12. This makes the notifications received be completely silent and appear only on the notification center without interrupting the user.实现您想要做的唯一方法是在询问设备上的通知权限时指定“临时”,这在 iOS >= 12 上受支持。这使得收到的通知完全静音,只出现在通知中心在不打扰用户的情况下。 However, the user can customize this and choose to be "interrupted" by the notifications, and this permission will also prevent all other notifications from "interrupting" the user.但是,用户可以对此进行自定义并选择被通知“打断”,并且此权限还将防止所有其他通知“打断”用户。

If you want to selectively specify some notifications to only go to the Notification Center, while others make sounds and show as notification alerts to the user, then the answer to your question is no, its not possible.如果您想有选择地指定某些通知仅转到通知中心,而其他通知发出声音并作为通知提醒显示给用户,那么您的问题的答案是否定的,这不可能的。

I am not sure if I understand your question right.我不确定我是否理解你的问题。 But if you just want to send a silent notification to iOS, wake up the app in background and let it handle the notification, this can be done, see the docs .但是,如果您只想向 iOS 发送静默通知,在后台唤醒应用程序并让它处理通知,则可以这样做,请参阅文档
Essentially, you have本质上,你有

  • to enable the remote notifications background mode in the Signing & Capabilities tab of your target, and在目标的签名和功能选项卡中启用远程通知后台模式,以及
  • to create a remote notification with an aps dictionary that includes only the content-available key:使用仅包含content-available键的 aps 字典创建远程通知:

    { "aps" : { "content-available" : 1 }, "key1" : "xxx", "key2" : 123 }

To receive the silent notification, the system will then wake your app in the background.为了接收静默通知,系统将在后台唤醒您的应用程序。 It then calls your app delegate's application(_:didReceiveRemoteNotification:fetchCompletionHandler:)然后它调用您的应用程序委托的application(_:didReceiveRemoteNotification:fetchCompletionHandler:)

是的,只发送它没有声音键。如果你添加空白的声音键,那么默认声音肯定会来,所以不要添加没有声音的键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM