iOS 推送通知没有声音

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26973415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 03:40:01  来源:igfitidea点击:

iOS Push Notification No Sound

iosaudiopush-notification

提问by Omer Janjua

This is the code to register for push

这是注册推送的代码

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

It works fine as the app registers with the server.

当应用程序向服务器注册时,它运行良好。

The PEM files are also done correctly as I can send a push to my device using sandbox APNS.

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

When I print my JSON payload from didReceiveRemoteNotificationI get this:

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

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

The issue is when I receive my push (even when the device is set to loud) it doesn't play a sound.

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

From my knowledge, if you don't specify a sound in the JSON payload it should play the default OS sound.

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

In my App notification settings on the phone the sound is enabled by default because when I register I specify UIUserNotificationTypeSound.

在手机上的应用通知设置中,声音默认处于启用状态,因为当我注册时,我指定了UIUserNotificationTypeSound.

Anyone else come across this issue?

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

回答by rckoenes

According to Apple's documentationyou need to specify defaultif you want to the default push notification to be played:

根据Apple 的文档,您需要指定default是否要播放默认推送通知:

The name of a sound file in the app bundle. The sound in this file is played as an alert. If the sound file doesn't exist or default is specified as the value, the default alert sound is played. The audio must be in one of the audio data formats that are compatible with system sounds; see Preparing Custom Alert Sounds for details.

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

The final JSON output:

最终的 JSON 输出:

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

回答by Oleg Gordiichuk

You should modify server JSON output to this. defaultit is sound type of the notification on your phone.

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

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

回答by Sushant Jagtap

for playing sound when our app receives push notification your json must contains sound attribute. so json like this

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

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