gpt4 book ai didi

ios - 使用 OneSignal 从收到的通知中设置角标(Badge)值

转载 作者:行者123 更新时间:2023-11-29 00:24:21 26 4
gpt4 key购买 nike

每当在用户之间(设备到设备)发送消息时,如果应用未处于焦点状态,则接收用户会收到通知。随着通知,该选项卡的标志值应增加 1。为了尝试这样做,我创建了一个 NotificationCenter 操作,该操作在 OneSignal 的 handleNotificationReceived block (在 initLaunchWithOptions 内) ) 就像这样:

handleNotificationReceived: { (notification) in
//Notification
NotificationCenter.default.post(name: MESSAGE_NOTIFICATION, object: nil)
print("Received Notification - \(notification?.payload.notificationID ?? "")")
},

观察者位于Messaging标签内,具有增加标签栏角标(Badge)的功能:

NotificationCenter.default.addObserver(自身,选择器:#selector(addBadge),名称:MESSAGE_NOTIFICATION,对象:nil)

//Adds a badge to the messages bar
func addBadge(){
self.navigationController?.tabBarController?.tabBar.items?[3].badgeValue = "1"
if #available(iOS 10.0, *) {
self.navigationController?.tabBarController?.tabBar.items?[3].badgeColor = ChatMessageCell.indexedColor
} else {
// Fallback on earlier versions
}
}

但是,我仍然无法获得用户出现的角标(Badge)值

最佳答案

这取决于您的 View Controller 层次结构是如何设置的。您尝试访问 badgeValue 的方式很可能没有设置它,因为其中一个可选属性返回 nil。在该行上设置一个断点并检查它们的值以了解是哪个。

如果您的 View Controller 嵌入在导航 Controller 中,并且该导航 Controller 是选项卡层次结构中的第一个子项,例如

UITabBarController -> UINavigationController -> UIViewController

然后您可以从 UIViewController 获取角标(Badge)值,如下所示 navigationController?.tabBarItem.badgeValue

navigationController 将返回最近的祖先 UINavigationController。如果这是选项卡层次结构中的第一个子 Controller ,那么它的 tabBarItem 属性将返回选项卡的 UITabBarItem,您可以在那里更新角标(Badge)值。

//Adds a badge to the messages bar
func addBadge(){
if let currentValue = navigationController?.tabBarItem.badgeValue {
let newValue = Int(currentValue)! + 1
navigationController?.tabBarItem.badgeValue = "\(newValue)"
} else {
navigationController?.tabBarItem.badgeValue = "1"
}

if #available(iOS 10.0, *) {
navigationController?.tabBarItem.badgeColor = ChatMessageCell.indexedColor
} else {
// Fallback on earlier versions
}
}

关于ios - 使用 OneSignal 从收到的通知中设置角标(Badge)值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43460979/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com