如果应用程序在ios中处于活动状态,如何在收到通知时发出声音?
请给我一个解决方案,当应用程序运行时,如果立即收到通知,我如何获得声音?我正在使用x-code 6.3?您可以手动添加声音文件,并在应用程序处于活动状态时播放如果应用程序在ios中处于活动状态,如何在收到通知时发出声音?,ios,objective-c,iphone,Ios,Objective C,Iphone,请给我一个解决方案,当应用程序运行时,如果立即收到通知,我如何获得声音?我正在使用x-code 6.3?您可以手动添加声音文件,并在应用程序处于活动状态时播放 -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { [[NSNotificationCenter defaultCenter] postNotificationName:@"
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"MessageReceived" object:self];
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
{
// NSString *cancelTitle = @"Close";
NSString *showTitle = @"Show";
// NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"New Order"
message:@"You just received new order"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:showTitle, nil];
UILocalNotification *localNotifcation = [[UILocalNotification alloc] init];
localNotifcation.userInfo = userInfo;
localNotifcation.soundName = UILocalNotificationDefaultSoundName;
// localNotifcation.alertBody = message;
localNotifcation.fireDate = [NSDate date];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotifcation];
[alertView show];
}
else {
//Do stuff that you would do if the application was not active
}
}
有关更多默认声音,请检查以下答案:
希望它有助于添加以下框架
if ([application applicationState] == UIApplicationStateActive) {
NSLog(@"active");
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"alarm2" ofType:@"wav"];
NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL, &_mySound);
AudioServicesPlaySystemSound(self.mySound);
}
或您可以使用系统声音-
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
{
SystemSoundID soundID;
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef ref = CFBundleCopyResourceURL(mainBundle, (CFStringRef)@"mySoundName.wav", NULL, NULL);
AudioServicesCreateSystemSoundID(ref, &soundID);
AudioServicesPlaySystemSound(soundID);
}
else {
// Push Notification received in the background
}
}
对于振动-
AudioServicesPlaySystemSound(1007);
查看所有声音列表 试试这段代码。在
AppDelegate
.m中输入didReceiveMotonification
的此代码
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
这段代码非常适合我。您可以使用带有自定义铃声的音频播放器来播放。我必须将mySoundName.wav文件添加到项目中?是的,否则您可以使用AudioServicesPlaySystemSound(1007)直接播放系统;请参阅我上面的帖子,了解声音代码AudioServicesPlaySystemSound(1007);有可能更改通知的标题吗?不,我认为不可能,它总是以标题形式显示您的姓名。有可能更改通知的标题吗?不可能。请阅读这些文件。
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
SystemSoundID soundID;
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef ref = CFBundleCopyResourceURL(mainBundle, (CFStringRef)@"Voicemail.wav", NULL, NULL);
AudioServicesCreateSystemSoundID(ref, &soundID);
AudioServicesPlaySystemSound(soundID);