xcode 如何在 iPhone 中接收推送通知时播放声音
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13513344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to play sound when receiving a Push notification in iPhone
提问by David G?lzh?user
Hi I am trying to play the default Push sound when receiving a Push Notification on my iDevice I used this code to play the sound in the
嗨,我正在尝试在我的 iDevice 上收到推送通知时播放默认的推送声音,我使用此代码在
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo` Method
NSDictionary *test =(NSDictionary *)[userInfo objectForKey:@"aps"];
NSString *alertString =(NSString *) [test objectForKey:@"alert"];
NSLog(@"String recieved: %@",alertString);
if (state == UIApplicationStateActive) {
UIAlertView *alertmessage=[[UIAlertView alloc]initWithTitle:@"iEverything Tech"
message:alertString delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertmessage show];
AudioServicesPlaySystemSound(1002);
}
if (state == UIApplicationStateInactive) {
AudioServicesPlaySystemSound(1002);
}
if (state == UIApplicationStateBackground) {
AudioServicesPlaySystemSound(1002);
}
And my second question is how to show the Pushed message in the AlertView?
我的第二个问题是如何在 AlertView 中显示 Pushed 消息?
Thank you for your answers!
谢谢您的回答!
And I can't use a Push provider like Parse because we hour own server and we need to push automatically
而且我不能使用像 Parse 这样的 Push 提供者,因为我们拥有自己的服务器,我们需要自动推送
回答by NSCry
You have to use push notification payload for playing sound.Read the apple doc.
您必须使用推送通知负载来播放声音。阅读苹果文档。
回答by jimpic
Like NSEncoder wrote, the sound has to be in the notification payload. To answer the second question, your notification will either be displayed in an alert, badge or not at all - depending on the setting in the users notification settings, you have no influence on this.
就像 NSEncoder 写的那样,声音必须在通知负载中。要回答第二个问题,您的通知将显示在警报、徽章中或根本不显示 - 根据用户通知设置中的设置,您对此没有影响。
回答by Ravi D
Try this to retrieve the notification message and "alertString" below holds the message received
试试这个来检索通知消息,下面的“alertString”保存收到的消息
NSDictionary *test =(NSDictionary *)[userInfo objectForKey:@"aps"];
NSString *alertString =(NSString *) [test objectForKey:@"alert"];
NSLog(@"String recieved: %@",alertString);
回答by Ravi D
Simply pass the string into the alert that will do
只需将字符串传递到警报中即可
NSDictionary *test =(NSDictionary *)[userInfo objectForKey:@"aps"];
NSString *alertString =(NSString *) [test objectForKey:@"alert"];
NSLog(@"String recieved: %@",alertString);
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Title" message:alertString delegate:self cancelButtonTitle:@"OK" otherButtonTitles: @"Not OK", nil] autorelease];
[alert show];
回答by Ravi D
In your "application didfinishlaunchingwithoptions add the following
在您的“应用程序 didfinishlaunchingwithoptions 中添加以下内容
NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotif) {
[self handleRemoteNotification:application userInfo:remoteNotif];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[UIApplication sharedApplication].applicationIconBadgeNumber--;
}
When you open the app through remote notification, by tapping on the notification this will decrease the badge number, if you want to remove the badge number when ever user opens the app then just implement the code under if condition, if condition here just checks if the application has been opened through by tapping on remote notifications ..,
当您通过远程通知打开应用程序时,通过点击通知,这将减少徽章编号,如果您想在用户打开应用程序时删除徽章编号,则只需在 if 条件下执行代码,如果此处的条件仅检查是否该应用程序已通过点击远程通知打开...,
回答by Tarika Chawla
In order to play a default sound for the notifications in iOS, you need to add the following code to the payload json
为了在 iOS 中播放通知的默认声音,您需要将以下代码添加到有效负载 json 中
"sound" : "default"
So, you "notification" payload should look something like:
因此,您的“通知”有效负载应该类似于:
"notification": {
"title": "4x8",
"body": "15:16.2342",
"message":"https://www.google.com",
"sound" : "default"
}