【问题标题】:iOS turn based match, push notifications not working, GKTurnBasedEventListener functions not callediOS 回合制比赛,推送通知不起作用,未调用 GKTurnBasedEventListener 函数
【发布时间】:2018-07-10 01:29:47
【问题描述】:

在我的 iOS 回合制比赛中,我正在尝试接收通知并获取

public func player(_ player: GKPlayer, receivedTurnEventFor match: GKTurnBasedMatch, didBecomeActive: Bool)

被调用,没有成功。

我将我的视图模型注册到本地播放器

 GKLocalPlayer.localPlayer().register(self)

我希望在其他玩家执行后触发

func endTurn(withNextParticipants nextParticipants: [GKTurnBasedParticipant], turnTimeout timeout: TimeInterval, match matchData: Data, completionHandler: ((Error?) -> Swift.Void)? = nil)

但没有成功。

如果我强制重新加载 matchData,那么我将获得第二个玩家刚刚提交的数据。所以 endTurn 工作正常。

是不是我做错了什么?

更新: 所以我创建了一个新项目,复制了我所有的文件, 在功能中仅启用了 Game Center。

当开发它运行完美时,我连接了两台设备(具有不同的苹果 ID)。通知正在工作,Turnbasedlistener 正在触发。

我一发布它进行内部测试,它就停止工作了!!!

【问题讨论】:

  • 您是否仔细检查过您的应用程序允许在“设置”下推送通知?如果用户决定不启用推送通知,则不会调用“receivedTurnEventFor”。
  • 是的,我做到了,允许推送通知已启用(徽章)
  • 所以我创建了一个新项目,复制了我所有的文件,在功能中只启用了 Game Center。当开发它运行完美时,我连接了两个设备(具有不同的苹果 ID)。通知正在工作,Turnbasedlistener 正在触发。我一发布它进行内部测试,它就停止工作了!!!

标签: ios swift gamekit gkturnbasedmatch


【解决方案1】:

我有非常相似的问题。我的解决方案是在等待轮到我时手动重新检查我的状态。 首先,我定义了全局变量var gcBugTimer: Timer

endTurn(withNextParticipants:turnTimeOut:match:completionHan‌​dler:)完成处理程序中:

let interval = 5.0
self.gcBugTimer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(self.isMatchActive), userInfo: nil, repeats: true)
self.gcBugTimer.tolerance = 1.0

上面的代码也应该被调用,以防玩家正在为新的比赛而高兴,而其他玩家正在轮流中。

然后定时器方法:

func isMatchActive() {
  // currentMatch - global variable contains information about current match 
  GKTurnBasedMatch.load(withID: currentMatch.matchID!) { (match, error) in
    if match != nil {
      let participant = match?.currentParticipant
      let localPlayer = GKLocalPlayer.localPlayer()
      if localPlayer.playerID == participant?.player?.playerID {
        self.player(localPlayer, receivedTurnEventFor: match!, didBecomeActive: false)
      }
    } else {
      print(error?.localizedDescription ?? "")
    }
  }
}

我在player(_:receivedTurnEventFor:didBecomeActive)的开头添加了以下代码:

if gcBugTimer != nil && gcBugTimer.isValid {
  gcBugTimer.invalidate()
}

【讨论】:

    【解决方案2】:

    最终对我有用的是在实际设备上进行测试,而不是在模拟器中。 receivedTurnEvents 函数似乎在模拟器中不起作用。

    Grigory 的解决方法非常适合使用模拟器进行测试。

    【讨论】:

      猜你喜欢
      • 2017-02-11
      • 1970-01-01
      • 2020-07-13
      • 1970-01-01
      • 1970-01-01
      • 2012-05-08
      • 2023-03-17
      • 1970-01-01
      相关资源
      最近更新 更多