使用 Sinch 和 Swift 进行 iOS 推送通知

标签 ios swift apple-push-notifications sinch

我使用 Sinch 和 Swift(带有桥接 header )进行即时消息传递,但在收到消息时没有收到任何推送通知。

  • 我拥有 Sinch Dashboard 的开发证书。
  • 后台模式已禁用。
  • 推送通知以其他方式工作(Pusher)

但是当我发送一条即时消息时,应用程序代理的“didReceiveRemoteNotification”函数永远不会被调用。

var sinClient:SINClient?
var push:SINManagedPush?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        //Sinch Push Notification
        sinClient = Sinch.client(withApplicationKey: "-----", applicationSecret: "------", environmentHost: "sandbox.sinch.com", userId: "----")
        sinClient?.setSupportMessaging(true)
        sinClient?.enableManagedPushNotifications()
        sinClient?.delegate = self
        sinClient?.messageClient().delegate = self
        sinClient?.start()
        sinClient?.startListeningOnActiveConnection()
        self.push = Sinch.managedPush(with: SINAPSEnvironment.development)
        self.push?.delegate = self
        self.push?.setDesiredPushTypeAutomatically()
        self.push?.registerUserNotificationSettings()
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let tokenParts = deviceToken.map { data -> String in
            return String(format: "%02.2hhx", data)
        }

        let token = tokenParts.joined()
        print("Device Token: \(token)")
        self.push?.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        self.push?.application(application, didReceiveRemoteNotification: userInfo)
    }
func managedPush(_ managedPush: SINManagedPush!, didReceiveIncomingPushWithPayload payload: [AnyHashable : Any]!, forType pushType: String!) {
        sinClient?.relayRemotePushNotification(payload)
    }

仅当我打开应用程序时才会收到即时消息。

最佳答案

最后发现问题出在我用模拟器发送消息

关于使用 Sinch 和 Swift 进行 iOS 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44420947/

相关文章:

ios - Swift Playgrounds 中的观看次数有限

ios - iOS-如何检测iDevice广告是否已接通电源

ios - 卡在为 MoonAPNS 创建 p12 文件

ios - 使用自动布局的 IB_DESIGNABLE 自定义 View 的错位 View 警告和奇怪行为

ios - 呈现 View Controller 时如何保持输入附件 View 向上

ios - 此 iOS SDK 不支持手机上的 iOS 版本

ios - 游戏在快速从后台恢复后退出暂停状态

php - APNS JSON PAYLOAD - 更多参数

ios - AFNetworking downloadTaskWithRequest :progress:destination:completionHandler: not writing file to the path

html - iOS HTML5 视频播放器下一个/上一个按钮