我构建了一个应用程序供亲密的家人和朋友使用。我无法将应用程序上传到 App Store,因为我使用的是受版权保护的材料/资产,因此我使用 TestFlight 来分发应用程序以允许我的家人和朋友使用它。 我已经使用 UserNotifications、UNUserNotificationCenter 等在应用程序中实现了本地通知。我每周安排两个通知,每个通知应在每周同一时间发送,每个通知在一周的不同日期发送。但是,我的个人设备是唯一真正收到这些通知的设备,并且通过 TestFlight 下载我的应用程序的每个“用户”都不会收到这些通知。我已验证我的“用户”确实为我的应用程序打开了通知。
最初,我关注了有关如何实现本地通知的 YouTube 视频。视频中显示的方法使用 UserNotification 的完成处理程序 API。我还尝试尝试使用异步 API,因为我的应用程序遵循异步等待架构,但这没有帮助。
虽然可能不是最佳实践,但我的实现如下:
我还请求用户允许发送这样的通知:
func checkForNotificationsPermission() {
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.getNotificationSettings { settings in
switch settings.authorizationStatus {
case .authorized:
self.dispatchNotifications()
case .notDetermined:
notificationCenter.requestAuthorization(options: [.alert, .sound]) { didAllow, error in
if didAllow {
self.dispatchNotifications()
}
}
default:
return
}
}
}
其中
dispatchNotifications()
遵循上述步骤 1-4。
我最好的假设是通知对我有用,因为我是与开发者帐户绑定的帐户,并且我有一些允许通知的证书,但我不确定。所有通过 TestFlight 使用我的应用程序的人都会以“营销”角色添加到我的 App Store Connect 中。
确保您在应用程序启动时请求通知权限。