【问题标题】:How to make sound when notification came if application is active in ios?如果应用程序在 ios 中处于活动状态,如何在收到通知时发出声音?
【发布时间】:2015-06-11 06:54:19
【问题描述】:
-(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 收到通知,我如何在应用程序运行时发出声音?

【问题讨论】:

  • 您可以使用带有自定义铃声的 AVAudioPlayer 来播放。

标签: ios objective-c iphone


【解决方案1】:

添加以下框架

#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所有声音列表。

【讨论】:

  • 我必须将 mySoundName.wav 文件添加到项目中?
  • 是的,否则你可以使用AudioServicesPlaySystemSound(1007)直接播放系统;声音代码请看我上面的帖子
  • AudioServicesPlaySystemSound(1007);
  • 通知标题可以改吗?
  • 不,我认为不可能,它总是显示你的名字作为标题。
【解决方案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

希望对你有帮助

【讨论】:

    【解决方案3】:

    试试这个代码。将此代码为didReceiveRemoteNotification 放入AppDelegate.m

    SystemSoundID soundID;
        CFBundleRef mainBundle = CFBundleGetMainBundle();
        CFURLRef ref = CFBundleCopyResourceURL(mainBundle, (CFStringRef)@"Voicemail.wav", NULL, NULL);
        AudioServicesCreateSystemSoundID(ref, &soundID);
        AudioServicesPlaySystemSound(soundID);
    

    这段代码非常适合我..

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多