【问题标题】:iOS - Push Notification with Sound onlyiOS - 仅带声音的推送通知
【发布时间】:2015-01-27 14:11:52
【问题描述】:

当我在我的应用程序未激活或关闭时收到推送通知时,我的设备会播放通知声音。当我的应用程序处于活动状态或打开状态时,它不会在收到通知/消息时播放声音。

我希望即使在应用程序处于活动/打开状态时也能播放通知声音。请问有人可以帮我实现吗?

  -(void)HandleNotification:(NSDictionary *)userInfo{

    NSMutableDictionary * test = [userInfo objectForKey:@"aps"];
    NSMutableDictionary *alert_msg = [test objectForKey:@"alert"];
    NoteType = [alert_msg objectForKey:@"type"];
    Note_Student_id = [alert_msg objectForKey:@"sid"];

    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"School Link"
                        message:[alert_msg objectForKey:@"title"]
                                                    delegate:self
                                           cancelButtonTitle:@"OK"
                                           otherButtonTitles:nil];
    [alert show];

}

我的通知如下所示:

  {
            aps =     {
                alert =         {
                    sid = 114;
                    title = fddsf;
                    type = 2;
                };
                sound = default;
            };
        }

【问题讨论】:

    标签: ios push-notification apple-push-notifications


    【解决方案1】:

    您必须在 HandleNotification 方法中播放声音,只需在里面添加即可。

    AudioServicesPlaySystemSound(1004);
    

    【讨论】:

    • 默认通知声音名称是什么
    • 谢谢你的工作,但最后我想要的声音就像通知声音一样,我尝试了iphonedevwiki.net/index.php/AudioServices的许多代码但无法得到这个
    • 试试AudioServicesPlayAlertSound(kUserPreferredAlert)
    • 我认为我之前的评论不起作用。您必须选择其中一种代码,或者创建自己的声音,因为 iOS SDK 不捆绑这些声音文件,您可以在设备上的 /System/Library/Audio/UISounds/ 文件夹中找到它们。但是,我相信这些文件是受版权保护的。
    【解决方案2】:

    试试这个:

    NSString* soundName = [[userInfo objectForKey: @"aps"] objectForKey:@"sound"];
    
    if([soundName isEqual: @"default"])
        soundName = @"default_sound_name.mp3";
    
    NSString *path = [[NSBundle mainBundle] pathForResource:soundName ofType:@""];
    SystemSoundID soundID;
    AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);
    AudioServicesPlaySystemSound(soundID);
    AudioServicesDisposeSystemSoundID(soundID);
    

    您无法访问通知默认声音,因此您应该将其下载到某个地方并将其添加到您的项目中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-05
      相关资源
      最近更新 更多