【问题标题】:iOS Push Notification No SoundiOS推送通知没有声音
【发布时间】:2015-01-14 09:47:04
【问题描述】:

这是注册推送的代码

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    [application registerForRemoteNotifications];
}
else
{
    [application registerForRemoteNotificationTypes: (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}

当应用程序向服务器注册时,它工作正常。

PEM 文件也正确完成,因为我可以使用沙盒 APNS 向我的设备发送推送。

当我从 didReceiveRemoteNotification 打印我的 JSON 有效负载时,我得到了这个:

{
    aps =     {
        alert = "Test Push Message";
    };
}

问题是当我收到推送时(即使设备设置为响亮),它也不会播放声音。

据我所知,如果您没有在 JSON 有效负载中指定声音,它应该播放默认的操作系统声音。

在我的手机应用通知设置中,默认情况下会启用声音,因为我在注册时指定了UIUserNotificationTypeSound

还有其他人遇到过这个问题吗?

【问题讨论】:

  • 你不应该检测类似的版本:if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0),而是检查类是否可用:if ([UIUserNotificationSettings class])

标签: ios audio push-notification


【解决方案1】:

根据Apple's documentation,如果要播放默认推送通知,需要指定default

应用程序包中声音文件的名称。该文件中的声音是 作为警报播放。如果声音文件不存在或默认为 指定为该值时,播放默认警报声音。音频 必须采用与兼容的音频数据格式之一 系统声音;有关详细信息,请参阅准备自定义警报声音。

最终的 JSON 输出:

{
    "aps" :     {
        "alert" : "Test Push Message",
        "sound" : "default"
    };
}

【讨论】:

  • 是的,我有声音并以这种方式振动......但我只想要声音。没有振动..比我怎么办?提前感谢 x...
  • 链接的文档不再存在。
  • 将代码的声音位与 Firebase 一起使用。效果很好
  • 这是用于 react-native 项目还是原生 ios?
  • @TM 没关系。 iOS 本身会摄取从 APNS 服务器传递的有效负载并对其进行处理。
【解决方案2】:

您应该将服务器 JSON 输出修改为此。 default是手机通知的声音类型。

{
    "aps": {
        "alert": "test",
        "sound": "default"
    }
}

【讨论】:

  • 是的,我有声音并以这种方式振动......但我只想要声音。没有振动..比我怎么办?提前感谢 x...
【解决方案3】:

当我们的应用收到推送通知时播放声音,您的 json 必须包含声音属性。所以json是这样的

{
    "aps":{
    "alert" :"your test message",
    "sound":"default"
        };
}

【讨论】:

    猜你喜欢
    • 2017-07-05
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多