UILocalNotification custom sound is not playing in iOS7

Jayeshkumar Sojitra picture Jayeshkumar Sojitra · Oct 12, 2013 · Viewed 12.6k times · Source

I'm using UILocalNotification in an application.

In the application there are two sounds which are played conditionally- I have applied proper conditions for them.

But when I install the application and run it on an iOS 7 device, then it fires the local notification but a sound is not playing in the application.

Code is given below to set the notification:

    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    if (localNotification == nil)
        return;
    localNotification.fireDate = [pickerView date];

    localNotification.timeZone = [NSTimeZone defaultTimeZone];

    if (alarm_number == 1) {
        localNotification.alertBody = [NSString stringWithFormat:@"First Alarm"];
    }
    else
    {
        localNotification.alertBody = [NSString stringWithFormat:@"Second Alarm"];
    }

    localNotification.alertAction =@"Ok";

    NSString *message = [[NSString alloc]initWithString:@""];

    if(alarm_number == 1)
    {
        localNotification.soundName=@"Alarm_1.mp3";
        message = @"First Alarm Scheduled";
    }
    else
    {
        localNotification.soundName=@"Alarm_2.mp3";
        message = @"Second Alarm Scheduled";
    }

    localNotification.applicationIconBadgeNumber = 1;

    // Specify custom data for the notification
    NSString *alarmString;
    if (alarm_number==1) {
        alarmString = [NSString stringWithFormat:@"First Alarm"];
    }
    else
    {
        alarmString = [NSString stringWithFormat:@"Second Alarm"];
    }

    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:alarmString forKey:@"AlarmFor"];

    localNotification.userInfo = infoDict;

    // Schedule the notification
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    [localNotification release];

What I have checked for is the Sound setting in the Settings/Notification Centre app for my app.

Please go through 1st to 3rd image to see what I have checked.

(1) Notification Center

(1) Notification Center

(2) Application

enter image description here

(3) Sound is off here

enter image description here

So, to enable this I have checked Inter App Audio at Capabilities in Targets of the application and it was Off as shown in the image below.

Capabilities in Inter-app audio

Capabilities in Inter-app audio

Then I have changed it to On and it looks like shown in the image below.

Capabilities after changed On

Yet, it still does not play any sound in iOS 7 devices.

Does anybody have any idea about why is it not working? It would be a great help.

Thanks.

Answer

wzhang84 picture wzhang84 · Mar 10, 2014

Did you try to turn off "Do not disturb" mode off? I have the same problem, then I found out "Do not Disturb" is on. After turn off that, it all works the right way.