当我的应用程序未激活或关闭时收到推送通知时,我的设备会播放通知声音。当我的应用程序处于活动状态或打开状态时,它在收到通知/消息时不会发出声音。
即使应用程序处于活动/打开状态,我也希望播放通知声音。请有人帮我实现吗?
-(void)HandleNotification:(NSDictionary *)userInfo{
NSMutableDictionary * test = [userInfo objectForKey:@"aps"];
NSMutableDictionary *alert_msg = [test objectForKey:@"alert"];
NoteType = [alert_msg objectForKey:@"type"];
Note_Student_id = [alert_msg objectForKey:@"sid"];
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"School Link"
message:[alert_msg objectForKey:@"title"]
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
我的通知如下:
{
aps = {
alert = {
sid = 114;
title = fddsf;
type = 2;
};
sound = default;
};
}
答案 0 :(得分:1)
您必须在HandleNotification方法中播放声音,只需添加内部。
AudioServicesPlaySystemSound(1004);
答案 1 :(得分:1)
试试这个:
NSString* soundName = [[userInfo objectForKey: @"aps"] objectForKey:@"sound"];
if([soundName isEqual: @"default"])
soundName = @"default_sound_name.mp3";
NSString *path = [[NSBundle mainBundle] pathForResource:soundName ofType:@""];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound(soundID);
AudioServicesDisposeSystemSoundID(soundID);
您无法访问通知默认声音,因此您应该将其下载到某处并将其添加到您的项目中