简体   繁体   中英

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. 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"
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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