首页 > 解决方案 > Pusher:iOS 推送通知未到达手机

问题描述

我正在尝试使用 Pusher 为我的应用程序发送推送通知。

我正在按照他们的教程(https://pusher.com/tutorials/push-notifications-ios/)进行设置。到目前为止,我已经在 XCode 中启用了推送通知,创建了一个实例并复制了它们的代码片段。

这是我的 AppDelegate.swift 文件(仅包括相关部分):

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    let pushNotifications = PushNotifications.shared

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        self.pushNotifications.start(instanceId: [instanceId)
        self.pushNotifications.registerForRemoteNotifications()
        try? self.pushNotifications.addDeviceInterest(interest: "hello")
        return true
    }


    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }
    
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        self.pushNotifications.registerDeviceToken(deviceToken)
    }

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        self.pushNotifications.handleNotification(userInfo: userInfo)
    }
}

我正在使用命令发送推送通知

curl -H "Content-Type: application/json" \
     -H "Authorization: Bearer $SECRET_KEY" \
     -X POST "https://INSTANCE_ID.pushnotifications.pusher.com/publish_api/v1/instances/INSTANCE_ID/publishes" \
     -d '{"interests":["hello"],"apns":{"aps":{"alert":{"title":"Hello","body":"Hello, world!"}}}}'

执行 curl 命令后,我收到以下消息:

{"publishId":[publishId]}

但是,我正在测试它的 iPhone 上没有显示任何通知。该应用程序正在后台运行。有什么帮助吗?

谢谢。

标签: iosswiftpush-notificationapple-push-notificationspusher

解决方案


确保您的 apns 证书有效,并且您已在 XCODE 中为推送通知设置了适当的功能


推荐阅读