2
-(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
    }
}

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?

Karthik Mandava
  • 507
  • 1
  • 14
  • 27

3 Answers3

2

You can manually add sound file and play it when app is 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);
    }

For more default sounds check this answer : Playing system sound without importing your own

Hope it helps

Community
  • 1
  • 1
Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
2

Add following framework

#include <AudioToolbox/AudioToolbox.h>

Use below code -

- (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);

For Vibration -

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

See here all list of sound.

Santu C
  • 2,644
  • 2
  • 12
  • 20
1

try this code. put this code for didReceiveRemoteNotification in AppDelegate.m

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

this code perfect work for me..

Jay Bhalani
  • 4,142
  • 8
  • 37
  • 50
  • Is there any possible to change title of notification ? – Karthik Mandava Jun 11 '15 at 07:48
  • Not possible. Please Read This documents. http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ApplePushService/ApplePushService.html – Jay Bhalani Jun 11 '15 at 08:38