简体   繁体   English

使用OneSignal从收到的通知中设置徽章值

[英]Setting a badge value from a received notification with OneSignal

Whenever a message is sent between users (device to device), the receiving user gets a notification if the app is not in focus. 每当用户之间(设备到设备)之间发送消息时,如果应用程序不在焦点上,接收方的用户就会收到通知。 Along with the notification, the badge value for that tab should increase by 1. In an attempt to do so, I've created a NotificationCenter action that goes off in OneSignal's handleNotificationReceived block (within initLaunchWithOptions ) like so: 与通知一起,该选项卡的标志值应增加1。为此,我创建了一个NotificationCenter操作,该操作在OneSignal的handleNotificationReceived块中(在initLaunchWithOptions内)关闭,如下所示

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

and the observer is located within the Messaging tab with a function that increases the tab bar badge: 并且观察者位于“消息传递”选项卡中,其功能是增加选项卡栏标志:

NotificationCenter.default.addObserver(self, selector: #selector(addBadge), name: MESSAGE_NOTIFICATION, object: 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
    }
}

However, Im still not able to get the badge value for the user to appear 但是,我仍然无法获得用户显示的徽章值

It depends on how your view controller hierarchy is set up. 这取决于您的视图控制器层次结构的设置方式。 The way you're trying to access badgeValue , it's likely that it's not being set because one of those optional properties is returning nil. 您尝试访问badgeValue的方式很可能未设置,因为这些可选属性之一返回nil。 Set a breakpoint on that line and inspect their values to learn which one. 在该行上设置一个断点,并检查其值以了解哪个值。

If your view controller is embedded in a navigation controller and that navigation controller is the first child in the tab hierarchy, like 如果您的视图控制器嵌入在导航控制器中,并且该导航控制器是选项卡层次结构中的第一个子级,例如

UITabBarController -> UINavigationController -> UIViewController UITabBarController-> UINavigationController-> UIViewController

then from the UIViewController you could get to the badge value like this navigationController?.tabBarItem.badgeValue . 然后可以从UIViewController获得徽章值,例如this navigationController?.tabBarItem.badgeValue

navigationController will return the nearest ancestor that is a UINavigationController. navigationController将返回最接近的祖先,即UINavigationController。 If that is the first child controller in the tab hierarchy then its tabBarItem property will return the UITabBarItem for the tab and you can update the badge value there. 如果这是选项卡层次结构中的第一个子控制器,则其tabBarItem属性将返回选项卡的UITabBarItem,您可以在那里更新徽章值。

//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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM