我正在尝试使用Firebase云消息传递实现推送通知,我通过firebase控制台发送消息。在Firebase控制台中撰写邮件时,我将徽章编号设置为1,如下图所示
之后,主屏幕上的我的应用程序图标将始终带有编号为#34; 1"的徽章,甚至很难我尝试卸载并重新安装它,带有编号的徽章" 1&#34 ;还在那里。
但只发生在我的iPhone ,如果我在另一部手机上安装它,徽章就不会显示
我在App委托中使用此代码来触发推送通知
@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
}
}
如何解决此问题?
答案 0 :(得分:2)
iOS将始终记住您的应用程序徽章数量,即使您卸载应用程序并再次安装它也是如此。要删除徽章,您必须执行以下任一操作,
badge = 0
向您的应用发送另一个推送通知。UIApplication.shared.applicationIconBadgeNumber = 0
打开您的应用时,您可以自行删除徽章计数。在Appdelegate's didBecomeActive(:)
方法中添加此行代码。感谢。