标签徽章仅在单击通知时有效

时间:2018-08-23 00:32:56

标签: ios swift firebase firebase-cloud-messaging

我正在使用FCM(GCM)进行通知,并且可以在前台和后台模式下显示。

如果用户在应用程序内,则会通过通知中心在标签控制器上更新徽章,因此会显示徽章,因此,如果收到通知并用户单击它,则徽章也会显示在标签栏中。

我遇到的问题是,如果用户不在应用程序外部,并且您没有通过单击图标或双击主屏幕按钮返回到应用程序,则不会立即单击通知,因此徽章不存在。如何实现这种功能?

应用代理

// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    let userInfo = notification.request.content.userInfo

    // With swizzling disabled you must let Messaging know about the message, for Analytics
    // Messaging.messaging().appDidReceiveMessage(userInfo)

    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")
        let content = notification.request.content
        print("userNotification willPresent")
        print(content.userInfo)
    }

    // Only display the notification outside the chat log controller
    if self.window?.rootViewController?.topViewController is ChatLogController {
        completionHandler([])
    } else{
        // Change this to your preferred presentation option
        let tab = self.getTopViewController() as? UITabBarController
        tab?.tabBar.items![1].badgeValue = "!"
        completionHandler([.alert,.sound])
    }
}

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")
        print("userNotification didReceive")
    }

    let tab = self.getTopViewController() as? UITabBarController
    tab?.tabBar.items![1].badgeValue = "!"

    // Print full message.
    print(userInfo)
    goToChatView()

    completionHandler()
}

0 个答案:

没有答案
相关问题