提问人:user16961399 提问时间:4/8/2023 最后编辑:HangarRashuser16961399 更新时间:4/8/2023 访问量:66
在IOS应用中,应用内通知因用户而异
Having In-App Notifications Vary By User in IOS App
问:
我正在为我的社交媒体应用程序处理应用程序内通知,我正在使用 Parse 和 UIKit 进行编程。我的主要问题是,虽然通知系统本身正在工作,但它并没有因用户而异。
例如,假设有两个用户,User1 和 User2。通知的设置方式是,当用户 1 喜欢用户 2 的帖子或对用户 2 的帖子发表评论时,只有用户 2 会收到通知,提示“用户 1 喜欢你的帖子!”或“用户 1 对你的帖子发表评论!”。但是,截至目前,当 User1 喜欢或评论 User2 的帖子时,User1 会收到通知。
下面是我的代码,它在我的应用程序中实现通知:
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)
}
}
一如既往,非常感谢所有帮助!
答: 暂无答案
评论