Ios 如何在使用多电话连接接收消息时发出声音通知?
我正在制作一个蓝牙聊天应用程序,其中我使用的是multipeer连接框架,我必须使用声音通知,并且消息将显示通知-Ios 如何在使用多电话连接接收消息时发出声音通知?,ios,objective-c,multipeer-connectivity,Ios,Objective C,Multipeer Connectivity,我正在制作一个蓝牙聊天应用程序,其中我使用的是multipeer连接框架,我必须使用声音通知,并且消息将显示通知- #import "MCManager.h" #include <AudioToolbox/AudioToolbox.h> #import "AppDelegate.h" @implementation MCManager -(id)init{ self = [super init]; if (self) { _peerID = nil
#import "MCManager.h"
#include <AudioToolbox/AudioToolbox.h>
#import "AppDelegate.h"
@implementation MCManager
-(id)init{
self = [super init];
if (self) {
_peerID = nil;
_session = nil;
_browser = nil;
_advertiser = nil;
}
return self;
}
#pragma mark - Public method implementation
-(void)setupPeerAndSessionWithDisplayName:(NSString *)displayName
{
_peerID = [[MCPeerID alloc] initWithDisplayName:displayName];
_session = [[MCSession alloc] initWithPeer:_peerID];
_session.delegate = self;
}
-(void)setupMCBrowser{
_browser = [[MCBrowserViewController alloc] initWithServiceType:@"chat-files" session:_session];
}
-(void)advertiseSelf:(BOOL)shouldAdvertise
{
if (shouldAdvertise)
{
_advertiser = [[MCAdvertiserAssistant alloc] initWithServiceType:@"chat-files"
discoveryInfo:nil
session:_session];
[_advertiser start];
}
else
{
[_advertiser stop];
_advertiser = nil;
}
}
#pragma mark - MCSession Delegate method implementation
-(void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state{
NSDictionary *dict = @{@"peerID": peerID,
@"state" : [NSNumber numberWithInt:state]
};
[[NSNotificationCenter defaultCenter] postNotificationName:@"MCDidChangeStateNotification"
object:nil
userInfo:dict];
}
-(void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID{
NSDictionary *dict = @{@"data": data,
@"peerID": peerID
};
[[NSNotificationCenter defaultCenter] postNotificationName:@"MCDidReceiveDataNotification"
object:nil
userInfo:dict];
SystemSoundID soundID;
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef ref = CFBundleCopyResourceURL(mainBundle, (CFStringRef)@"Voicemail.wav", NULL, NULL);
AudioServicesCreateSystemSoundID(ref, &soundID);
AudioServicesPlaySystemSound(soundID);
}
-(void)session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progress{
NSDictionary *dict = @{@"resourceName" : resourceName,
@"peerID" : peerID,
@"progress" : progress
};
[[NSNotificationCenter defaultCenter] postNotificationName:@"MCDidStartReceivingResourceNotification"
object:nil
userInfo:dict];
dispatch_async(dispatch_get_main_queue(), ^{
[progress addObserver:self
forKeyPath:@"fractionCompleted"
options:NSKeyValueObservingOptionNew
context:nil];
});
}
-(void)session:(MCSession *)session didFinishReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID atURL:(NSURL *)localURL withError:(NSError *)error{
NSDictionary *dict = @{@"resourceName" : resourceName,
@"peerID" : peerID,
@"localURL" : localURL
};
[[NSNotificationCenter defaultCenter] postNotificationName:@"didFinishReceivingResourceNotification"
object:nil
userInfo:dict];
}
-(void)session:(MCSession *)session didReceiveStream:(NSInputStream *)stream withName:(NSString *)streamName fromPeer:(MCPeerID *)peerID
{
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"MCReceivingProgressNotification"
object:nil
userInfo:@{@"progress": (NSProgress *)object}];
}
@end
#导入“MCManager.h”
#包括
#导入“AppDelegate.h”
@实现MCManager
-(id)init{
self=[super init];
如果(自我){
_皮里德=零;
_会话=零;
_浏览器=零;
_广告客户=零;
}
回归自我;
}
#pragma标记-公共方法实现
-(void)setupPeerAndSessionWithDisplayName:(NSString*)displayName
{
_peerID=[[McPeerind alloc]initWithDisplayName:displayName];
_session=[[MCSession alloc]initWithPeer:_peerID];
_session.delegate=self;
}
-(无效)设置浏览器{
_browser=[[MCBrowserViewController alloc]InitWithService类型:@“聊天文件”会话:_会话];
}
-应该做广告吗
{
如果(应该做广告)
{
_广告客户=[[McAdvertiseAssistant alloc]InitWithService类型:@“聊天文件”
发现信息:无
会议:_会议];
[_广告商开始];
}
其他的
{
[广告商停止];
_广告客户=零;
}
}
#pragma标记-MCSession委托方法实现
-(void)session:(MCSession*)session peer:(mcpeirid*)peerID didChangeState:(MCSessionState)state{
NSDictionary*dict=@{@“peerID”:peerID,
@“状态”:[NSNumber numberWithInt:state]
};
[[NSNotificationCenter defaultCenter]postNotificationName:@“MCDidChangeStateNotification”
对象:无
用户信息:dict];
}
-(void)session:(MCSession*)session didReceiveData:(NSData*)来自peer的数据:(MCPERID*)peerID{
NSDictionary*dict=@{@“数据”:数据,
@“皮瑞德”:皮瑞德
};
[[NSNotificationCenter defaultCenter]postNotificationName:@“MCDidReceiveDataNotification”
对象:无
用户信息:dict];
系统声音ID;
CFBundleRef mainBundle=CFBundleGetMainBundle();
CFURLRef-ref=CFBundleCopyResourceURL(mainBundle,(CFStringRef)@“Voicemail.wav”,NULL,NULL);
AudioServicesCreateSystemSoundID(参考和soundID);
AudioServicesPlaySystemSound(soundID);
}
-(void)session:(MCSession*)session didStartReceivingResourceWithName:(NSString*)resourceName fromPeer:(mcpeirId*)peerID withProgress:(NSProgress*)progress{
NSDictionary*dict=@{@“resourceName”:resourceName,
@“peerID”:peerID,
@“进展”:进展
};
[[NSNotificationCenter defaultCenter]postNotificationName:@“MCDidStartReceivingResourceNotification”
对象:无
用户信息:dict];
dispatch\u async(dispatch\u get\u main\u queue()^{
[进展情况:自我评价]
forKeyPath:@“fractionCompleted”
选项:NSKeyValueObservingOptionNew
上下文:无];
});
}
-(void)session:(MCSession*)session未完成接收ResourceWithName:(NSString*)来自对等方的resourceName:(mcpeirId*)peerID atURL:(NSURL*)本地URL withError:(NSError*)错误{
NSDictionary*dict=@{@“resourceName”:resourceName,
@“peerID”:peerID,
@“localURL”:localURL
};
[[NSNotificationCenter defaultCenter]postNotificationName:@“didFinishReceivingResourceNotification”
对象:无
用户信息:dict];
}
-(void)session:(MCSession*)session didReceiveStream:(NSInputStream*)名为:(NSString*)streamName fromPeer:(mcPeerind*)peerID的流
{
}
-(void)observeValueForKeyPath:(NSString*)对象的键路径:(id)对象更改:(NSDictionary*)更改上下文:(void*)上下文
{
[[NSNotificationCenter defaultCenter]postNotificationName:@“MCReceivingProgressNotification”
对象:无
用户信息:@{@“进度”:(NSProgress*)对象}];
}
@结束
我在didReceiveData
中编写此代码,以便在收到数据时通知声音,但它不起作用。我正在发送消息,但没有发出任何声音通知。
请帮帮我,我从过去10天就陷入了这个问题。
谢谢试试这个:
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"Voicemail" ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
AudioServicesPlaySystemSound (soundID);
您必须在有效负载中传递声音文件名,否则默认通知音为default.mp3。@AayushKatiyar如果有效,您能检查我的答案是否有效吗?