简体   繁体   English

当我收到推送通知时如何播放声音(应用正在运行时)

[英]How to play sound when i received push notification(when app is running)

I am implementing push notification in my application. 我正在应用程序中实现推送通知。 I want to play a sound when i receive push notification even when the application is running(currently am getting notification alert only not sound) or active state. 我希望在即使应用程序正在运行(当前未收到通知警报)或处于活动状态也收到推送通知时播放声音。 Currently the device sound is playing when receive notification when the application is running at background(ie after it get un lock) only. 当前,仅当应用程序在后台运行时(即解锁后),才在接收通知时播放设备声音。 Why the device sound is not playing when the app is receive notification at active state? 当应用程序在活动状态下收到通知时,为什么设备声音没有播放?

Please help me. 请帮我。 Thanks in advance. 提前致谢。

When your app is open, you will want to respond to the push notification and play a sound yourself. 当您的应用打开时,您将希望响应推送通知并自己播放声音。 If the app is open, by default, nothing is displayed and no sound is played, even if it does when your app is closed. 如果应用程序处于打开状态,则默认情况下,即使关闭应用程序时也不会显示任何内容且不会播放声音。

In your app delegeate, respond to the application:didReceiveRemoteNotification: message and you will get the notification send. 在您的应用程序委托中,响应application:didReceiveRemoteNotification: didReceiveRemoteNotification application:didReceiveRemoteNotification:消息,您将收到通知发送。 From there, you can initiate a sound playing: 从那里,您可以启动声音播放:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NSLog(@"got remote notification %@", userInfo);
    // now play a sound here...
}

Maybe you can play sound from within where the alert is called aswell? 也许您也可以在称为警报的地方播放声音?

Like 喜欢

Alert
{//alert text
}

and then here call the sound code right away after the alert within the method that alert is. 然后在该警报所在的方法内的警报之后立即调用声音代码。

Sound for push notification can be provided by push service. 推送服务可以提供用于推送通知的声音。 The payload for a simple push message with default sound will looks like this: 具有默认声音的简单推送消息的有效负载如下所示:

{
    "aps":
    {
        "alert": "Hello, world!",
        "sound": "default"
    }
}

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

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