繁体   English   中英

iOS-仅带声音的推送通知

[英]iOS - Push Notification with Sound only

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

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

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

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

AudioServicesPlaySystemSound(1004);

尝试这个:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM