app在前台时 如何播放自定义推送消息提示声

http://blog.csdn.net/rolandMan_/article/details/47837797

一般如果修改了apple官方的推送声音后,则APP进入后台后,推送会播放开发者自定制的推送声音,而用户在使用APP(也就是APP运行时)的时候,一般是不会有推送声音,因为此时的推送内容已经呈现在用户眼前了。因此,要使用户在使用的时候有推送声音,我们必须在接受推送的时候播放自己定制的声音。

一、怎么修改苹果原生的推送声音?(APP进入后台才有效果)

1、将声音文件格式转成.wav格式(不知道怎么转,百度去),然后加入到Xcode项目中。 
2、服务器给你推送的userinfo中sound字段的value必须和你的声音文件名一致,这样就成功修改了苹果原生的推送声音。

二、怎么在APP运行时也能响应自定制推送声音?

#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
static SystemSoundID push = 0;

-(void) playSound
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"push" ofType:@"wav"];
    NSLog(@"path = %@",path);
    if (path) {
        //注册声音到系统
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path],&push);
        AudioServicesPlaySystemSound(push);
//        AudioServicesPlaySystemSound(push);//如果无法再下面播放,可以尝试在此播放
    }

    AudioServicesPlaySystemSound(push);   //播放注册的声音,(此句代码,可以在本类中的任意位置调用,不限于本方法中)

    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);   //让手机震动
}

把[play sound]这个方法加到你成功接收推送的那个方法中didReceiveRemoteNotification

如果需要及时停止铃声  调用下面方法:

AudioServicesDisposeSystemSoundID(push);


猜你喜欢

转载自blog.csdn.net/u010462316/article/details/79554018