如果应用程序在ios中处于活动状态时通知如何发出声音? [英] How to make sound when notification came if application is active in ios?

查看:143
本文介绍了如果应用程序在ios中处于活动状态时通知如何发出声音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-(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,我怎样才能在应用程序运行时发出声音?

Please give me a solution how can i get sound when application is running if notification came right now am using x-code 6.3?

推荐答案

添加以下框架

#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
    }
}

您可能使用系统声音 -

Or You may used system sound -

AudioServicesPlaySystemSound(1007);

振动 -

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

参见这里所有声音列表。

这篇关于如果应用程序在ios中处于活动状态时通知如何发出声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆