在 IOS App 中应用内通知因用户而异

问题描述 投票:0回答:0

我正在为我正在使用 Parse 和 UIKit 编程的社交媒体应用程序处理应用程序内通知。我的主要问题是,虽然通知系统本身正在运行,但它不会因用户而异。

例如,假设有两个用户,User1 和User2。应该设置通知的方式是当用户 1 喜欢用户 2 的帖子或对用户 2 的帖子发表评论时,只有用户 2 收到通知说“用户 1 喜欢你的帖子!”或“User1 对您的帖子发表了评论!”。然而,截至目前,当用户 1 喜欢或评论用户 2 的帖子时,用户 1 会收到通知。

这是我在我的应用程序中实现通知的代码:

import UIKit
import Parse

class NotificationViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
{
    @IBOutlet weak var tableView: UITableView!
    var notifications: [PFObject] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self

        // Listen for post like events
        NotificationCenter.default.addObserver(self, selector: #selector(didLikePost(_:)), name: NSNotification.Name("PostLikedByOtherUser"), object: nil)
        
        // Listen for comment events
        NotificationCenter.default.addObserver(self, selector: #selector(madeCommentOnPost(_:)), name: NSNotification.Name("CommentMade"), object: nil)

        // Configure Notification class on Parse dashboard
        let notificationClass = PFObject(className: "Notification")
        notificationClass["user"] = PFUser.current()
        notificationClass["fromUser"] = PFUser.current()
        notificationClass["post"] = PFObject(className: "Post")
        notificationClass.saveInBackground { (success, error) in
            if success {
                print("Notification class created successfully")
            } else if let error = error {
                print("Error creating notification class: \(error.localizedDescription)")
            }
        }
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        // Query for notifications from Parse
        let query = PFQuery(className: "Notification")
        query.whereKey("user", equalTo: PFUser.current()!)
        query.includeKey("fromUser")
        query.includeKey("post")
        query.order(byDescending: "createdAt")
        query.findObjectsInBackground { (objects, error) in
            if let error = error {
                print("Error querying for notifications: \(error.localizedDescription)")
            } else if let notifications = objects {
                self.notifications = notifications
                self.tableView.reloadData()
            }
        }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return notifications.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "NotificationCell", for: indexPath) as! NotificationCell

        let notification = notifications[indexPath.row]
        let fromUser = notification["fromUser"] as! PFUser
        let post = notification["post"] as? PFObject

        cell.usernameLabel.text = fromUser.username

        if let username = fromUser.username
        {
            if post != nil
            {
                cell.messageLabel.text = "\(username) liked your post."
            } else
            {
                cell.messageLabel.text = "\(username)left a comment!"
            }
        }

        return cell
    }

    @objc func didLikePost(_ notification: Notification)
    {
        guard let post = notification.object as? PFObject,
            let postOwner = post["user"] as? PFUser,
            let liker = notification.userInfo?["liker"] as? PFUser,
            postOwner.objectId != liker.objectId,
            postOwner.objectId != PFUser.current()?.objectId else
        {
                return
        }

        // Create a new notification object
        let notificationObject = PFObject(className: "Notification")
        notificationObject["user"] = postOwner
        notificationObject["fromUser"] = liker
        notificationObject["post"] = post

        notificationObject.saveInBackground { (success, error) in
            if success
            {
                print("Successfully created notification")
            } else if let error = error
            {
                print("Error creating notification: \(error.localizedDescription)")
            }
        }
    }

    // Called when a comment is made
    @objc func madeCommentOnPost(_ notification: Notification)
    {
        guard let comment = notification.object as? PFObject,
            let post = comment["post"] as? PFObject,
            let postOwner = post["user"] as? PFUser,
            let commenter = notification.userInfo?["commenter"] as? PFUser,
            postOwner.objectId != commenter.objectId,
            postOwner.objectId != PFUser.current()?.objectId else
        {
                return
        }

        // Create a new notification object
        let notificationObject = PFObject(className: "Notification")
        notificationObject["user"] = postOwner
        notificationObject["fromUser"] = commenter
        notificationObject["post"] = post

        notificationObject.saveInBackground { (success, error) in
            if success
            {
                print("Successfully created notification")
            } else if let error = error
            {
                print("Error creating notification: \(error.localizedDescription)")
            }
        }
    }

    deinit
    {
        NotificationCenter.default.removeObserver(self)
    }
}

一如既往,非常感谢所有帮助!

ios swift parse-platform notifications uikit
© www.soinside.com 2019 - 2024. All rights reserved.