【问题标题】:How to play sound when receiving a Push notification in iPhone在 iPhone 中接收推送通知时如何播放声音
【发布时间】:2012-11-22 13:04:08
【问题描述】:

您好,我正在尝试在我的 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 这样的推送提供程序,因为我们拥有自己的服务器,并且需要自动推送

【问题讨论】:

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


    【解决方案1】:
    【解决方案2】:

    就像 NSEncoder 写的那样,声音必须在通知负载中。要回答第二个问题,您的通知将显示在警报、徽章或根本不显示 - 根据用户通知设置中的设置,您对此没有影响。

    【讨论】:

      【解决方案3】:

      试试这个来检索通知消息,下面的“alertString”保存收到的消息

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

      【讨论】:

      • 感谢您的回答,但我喜欢在 AlertView 中查看消息,而不是在 Xcode 的登录中查看消息。我使用它,但随后应用程序崩溃。检查我添加的问题。
      • 好的,谢谢我用你的帖子解决了问题,坦克!我将代码添加到我的问题中
      【解决方案4】:

      只需将字符串传递到警报中即可

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

      【讨论】:

      • 另一个问题你知道我打开应用程序时如何删除獾吗?我试过这个 [UIApplication sharedApplication].applicationIconBadgeNumber = 0;但它根本不起作用。
      【解决方案5】:

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

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

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

      【讨论】:

        【解决方案6】:

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

        "sound" : "default"
        

        因此,您的“通知”有效负载应如下所示:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-08-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多