我正在尝试使用 Firebase 云消息传递实现推送通知,我通过 firebase 控制台发送消息。在 Firebase 控制台中撰写消息时,我将角标(Badge)编号设置为 1,如下图所示
在那之后,我的应用程序在主屏幕上的图标总是有数字“1”的角标(Badge),即使我尝试卸载并重新安装它,数字“1”的角标(Badge)仍然存在。
但它只发生在我的 iPhone 上,如果我在其他手机上安装它,角标(Badge)将不会显示
我在 App delegate 中使用这段代码来触发推送通知
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate, UNUserNotificationCenterDelegate {
var window: UIWindow?
var fcmTokenUser : String?
var firsTimeUsingApp = true
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
print(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as String)
// To get FCM token that will be sent to APNS via Google FCM
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
Messaging.messaging().delegate = self
let token = Messaging.messaging().fcmToken
fcmTokenUser = token
checkFirstTimeUsingAppOrNot()
moveToNextPage()
// to make status bar in the light mode (in info.plist it also has to be set 'View controller-based status bar appearance' to NO)
UIApplication.shared.statusBarStyle = .lightContent
return true
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String){
// This callback is fired at each app startup (when the user install the app for the very first time) and whenever a new token is generated due to The app is restored on a new device, The user uninstalls/reinstall the app, The user clears app data.
// after fcm generated for the very first time,then fcm can also be retrieved in the 'didFinishLaunchingWithOptions' method above (let token = Messaging.messaging().fcmToken)
fcmTokenUser = fcmToken
moveToNextPage()
}
private func application(application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
Messaging.messaging().apnsToken = deviceToken as Data
}
}
如何解决这个问题?
最佳答案
iOS 会始终记住您的应用程序角标(Badge)计数,即使您卸载应用程序并重新安装也是如此。要移除角标(Badge),您必须执行以下任一操作,
- 使用
badge = 0
向您的应用发送另一个推送通知。 - 使用以下代码
UIApplication.shared.applicationIconBadgeNumber = 0
每当用户打开您的应用程序时,您自己删除角标(Badge)计数。在Appdelegate 的 didBecomeActive(:)
方法中添加这行代码。
谢谢。
关于ios - 为什么我收到通知后无法移除角标(Badge)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49354965/