推送通知到达时不播放自定义声音
Custom sound not played when push notification arrives
我在为我的应用设置消息声音时遇到了一些问题。
我最近从事推送通知方面的工作。通知工作正常。但是当推送通知到达时我无法添加自定义声音。接收负载数据到服务器。在哪个推送通知委托方法中添加自定义响铃代码。
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{
}
请使用此代码,它会帮助你
- (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{
}
}
我在为我的应用设置消息声音时遇到了一些问题。
我最近从事推送通知方面的工作。通知工作正常。但是当推送通知到达时我无法添加自定义声音。接收负载数据到服务器。在哪个推送通知委托方法中添加自定义响铃代码。
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{
}
请使用此代码,它会帮助你
- (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{
}
}