2016-06-20 7 views
0

Ich habe Probleme, den Nachrichtenton für meine App einzustellen.Benutzerdefinierter Sound wird nicht abgespielt, wenn die Push-Benachrichtigung ankommt

Ich bin neu auf Push-Benachrichtigung arbeiten. Die Benachrichtigung funktioniert einwandfrei. Ich kann jedoch keinen benutzerdefinierten Sound hinzufügen, wenn die Push-Benachrichtigung eintrifft. Empfangen von Nutzdaten zum Server In welcher Push-Benachrichtigung Delegate-Methode zum Hinzufügen von benutzerdefinierten Ring-Code.

Code 

    - (BOOL)application:(UIApplication *)application  didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

    UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound); 
    UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil]; 
    [application registerUserNotificationSettings:setting]; 
    [application registerForRemoteNotifications]; 


    NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; 

    if (notification) { 
     NSString *cancelTitle = @"Reject"; 
     NSString *showTitle = @"Accept"; 
     NSString *message = @"This is Testing"; 
     UIAlertView *alertView = [[UIAlertView alloc]  initWithTitle:@"Order" 
                  message:message 
                  delegate:self 
                cancelButtonTitle:cancelTitle 
                otherButtonTitles:showTitle, nil]; 
     [alertView show]; 
    } else { 
     // from the springboard 
    } 

    return YES; 
} 
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{ 
     UIApplicationState state = [application applicationState]; 
     if (state == UIApplicationStateActive) { 

      NSString *ring = [NSString stringWithFormat:@"%@",[[userInfo objectForKey:@"aps"] objectForKey:@"sound"]]; 

      NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] resourcePath],ring]]; 

      NSError *error; 

      audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; 
      audioPlayer.numberOfLoops = 0; 
      [audioPlayer play]; 


      NSString *cancelTitle = @"Close"; 
      NSString *showTitle = @"Show"; 
      NSString *message = @"This is Testing"; 
      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Some title" 
                   message:message 
                   delegate:self 
                 cancelButtonTitle:cancelTitle 
                 otherButtonTitles:showTitle, nil]; 
      [alertView show]; 
      }else{ 



      } 
+0

, welche Art der Tondatei, die Sie verwendet haben? , ich meine .caf, .wav usw. – iMHitesh

+0

sobald Prüfdatei vorhanden ist oder nicht, die aus Benachrichtigungsdaten kommt und keine Notwendigkeit, NSBundle Hauptbündel Ressourcenpfad geben, wenn es im Hauptbündel vorhanden ist, können Sie direkt Dateiname geben –

+0

Sie geben können [NSURL fileURLWithPath: ring] –

Antwort

0

Bitte verwenden Sie diesen Code, wird es Sie werden helfen

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
    { 
      UIApplicationState state = [application applicationState]; 
      if (state == UIApplicationStateActive) { 

      //  NSString *ring = [NSString stringWithFormat:@"%@",[[userInfo objectForKey:@"aps"] objectForKey:@"sound"]]; 

     //  NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] resourcePath],ring]]; 

      // NSError *error; 

      //  audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; 
     //  audioPlayer.numberOfLoops = 0; 
     //  [audioPlayer play]; 

     NSString *path =[[NSBundle mainBundle] pathForResource:@"circuit___ios_7" ofType:@"mp3"]; 
       SystemSoundID soundID; 
       AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID); 

     AudioServicesPlaySystemSound(soundID); 

       NSString *cancelTitle = @"Close"; 
       NSString *showTitle = @"Show"; 
       NSString *message = @"This is Testing"; 
       UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Some title" 
                    message:message 
                    delegate:self 
                  cancelButtonTitle:cancelTitle 
                  otherButtonTitles:showTitle, nil]; 
       [alertView show]; 
       }else{ 



       } 
} 
Verwandte Themen