-(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
}
}
请给我一个解决方案,如果现在通知正在使用x-code 6.3,我怎样才能在应用程序运行时发出声音?
答案 0 :(得分:2)
您可以手动添加声音文件,并在应用处于活动状态时播放。
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);
}
如需更多默认声音,请查看以下答案:Playing system sound without importing your own
希望有所帮助
答案 1 :(得分:2)
添加以下框架
#include <AudioToolbox/AudioToolbox.h>
使用以下代码 -
- (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);
振动 -
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
请参阅here所有声音列表。
答案 2 :(得分:1)
试试这段代码。将didReceiveRemoteNotification
的此代码放在AppDelegate
。m
SystemSoundID soundID;
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef ref = CFBundleCopyResourceURL(mainBundle, (CFStringRef)@"Voicemail.wav", NULL, NULL);
AudioServicesCreateSystemSoundID(ref, &soundID);
AudioServicesPlaySystemSound(soundID);
这段代码对我来说是完美的工作..