Я новый разработчик и настроил OneSignal с моим приложением для iOS, а моя база данных - Firebase, однако я изо всех сил пытаюсь найти, как отправить push -уведомления определенным пользователям, когда они получат новое следствие, может ли кто -нибудь помочь
Это коды: < /p>
static func follow(uid: String) async throws {
guard let currentUid = Auth.auth().currentUser?.uid else { return }
let userFollowingDoc = FirestoreConstants.FollowingCollection.document(currentUid).collection("user-following").document(uid)
let userFollowersDoc = FirestoreConstants.FollowersCollection.document(uid).collection("user-followers").document(currentUid)
try await Firestore.firestore().runTransaction { transaction, errorPointer in
do {
let userDoc = FirestoreConstants.UserCollection.document(uid)
let currentUserDoc = FirestoreConstants.UserCollection.document(currentUid)
transaction.updateData(["stats.followers": FieldValue.increment(Int64(1))], forDocument: userDoc)
transaction.updateData(["stats.following": FieldValue.increment(Int64(1))], forDocument: currentUserDoc)
transaction.setData([:], forDocument: userFollowingDoc)
transaction.setData([:], forDocument: userFollowersDoc)
} catch {
errorPointer?.pointee = error as NSError
return nil
}
return nil
}
}
import UserNotifications
import OneSignalExtension
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
var receivedRequest: UNNotificationRequest!
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
self.receivedRequest = request
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
guard let bestAttemptContent = bestAttemptContent else { return }
// Configure the notification content
OneSignalExtension.didReceiveNotificationExtensionRequest(
self.receivedRequest,
with: bestAttemptContent,
withContentHandler: contentHandler
)
// Add custom notification handling if needed
if let userInfo = bestAttemptContent.userInfo as? [String: Any] {
// Example: Add a custom category for follow notifications
if let notificationType = userInfo["type"] as? String, notificationType == "follow" {
bestAttemptContent.categoryIdentifier = "FOLLOW_CATEGORY"
}
// Example: Modify the notification body based on the type
if let senderName = userInfo["senderName"] as? String {
bestAttemptContent.body = "\(senderName) \(bestAttemptContent.body)"
}
}
// Add a custom sound
bestAttemptContent.sound = UNNotificationSound.default
}
override func serviceExtensionTimeWillExpire() {
// Called just before the extension will be terminated by the system.
guard let contentHandler = contentHandler,
let bestAttemptContent = bestAttemptContent else { return }
// Use this as an opportunity to deliver your "best attempt" at modified content
OneSignalExtension.serviceExtensionTimeWillExpireRequest(
self.receivedRequest,
with: bestAttemptContent
)
contentHandler(bestAttemptContent)
}
}
Подробнее здесь: https://stackoverflow.com/questions/794 ... llow-swift
OneSignal Отправить push -уведомление пользователям, когда они получат новое следующее Swift ⇐ IOS
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение