【发布时间】:2021-09-20 01:45:19
【问题描述】:
我正在尝试使用 Pusher 为我的应用发送推送通知。
我正在按照他们的教程 (https://pusher.com/tutorials/push-notifications-ios/) 进行设置。到目前为止,我已经在 XCode 中启用了推送通知,创建了一个实例并复制了他们的代码 sn-p。
这是我的 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 上没有显示任何通知。该应用程序正在后台运行。有什么帮助吗?
谢谢。
【问题讨论】:
-
当应用在前台时,通知是否显示在 iPhone 上?
-
不,不是。它说它已在终端中发布(因此为 publishId),但从未出现在我的 iPhone 上。
-
@DarrelGulseth 要获得更快的解决方案,请分享具有相同代码设置的示例应用程序的 Github 链接。
标签: ios swift push-notification apple-push-notifications pusher