如何在iPhone中接收推送通知时播放声音

时间:2012-11-22 13:04:09

标签: ios xcode uialertview apple-push-notifications audio

嗨我正在尝试在我的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);
        }

我的第二个问题是如何在AlertView中显示Pushed消息?

感谢您的回答!

我不能像Parse一样使用Push提供程序,因为我们需要自动推送服务器

6 个答案:

答案 0 :(得分:2)

答案 1 :(得分:2)

与NSEncoder写的一样,声音必须在通知有效载荷中。要回答第二个问题,您的通知将显示在警告,徽章或根本不显示 - 取决于用户通知设置中的设置,您对此没有任何影响。

答案 2 :(得分:1)

尝试此操作以检索通知消息,并且下面的“alertString”保留收到的消息

NSDictionary *test =(NSDictionary *)[userInfo objectForKey:@"aps"];
NSString *alertString =(NSString *) [test objectForKey:@"alert"];
NSLog(@"String recieved: %@",alertString);

答案 3 :(得分:0)

只需将字符串传递到将要执行的警报

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

答案 4 :(得分:0)

在您的“应用程序didfinishlaunchingwithoptions中添加以下内容

NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotif) {
    [self handleRemoteNotification:application userInfo:remoteNotif];
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    [UIApplication sharedApplication].applicationIconBadgeNumber--;        
}

当您通过远程通知打开应用程序时,通过点击通知,这将减少徽章编号,如果您想在用户打开应用程序时删除徽章编号,那么只需在条件下执行代码,如果条件在这里只需通过点击远程通知来检查应用程序是否已打开..,

答案 5 :(得分:0)

要在iOS中播放通知的默认声音,您需要将以下代码添加到有效负载json

"sound" : "default"

所以,你"通知"有效载荷应该类似于:

 "notification": {
        "title": "4x8",
        "body": "15:16.2342",
        "message":"https://www.google.com",
        "sound" : "default"
      }