我正在尝试使用 Firebase Cloud Messaging 实现推送通知,我通过 firebase 控制台发送消息。在 Firebase 控制台中撰写消息时,我将徽章编号设置为 1,如下图所示
此后,我在主屏幕上的应用程序图标将始终带有数字“1”的徽章,即使我尝试卸载并重新安装它,数字“1”的徽章仍然存在。
但是它只发生在我的iPhone上,如果我将其安装在另一部手机上,徽章将不会显示
我在应用程序委托中使用此代码来触发推送通知
@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 = 0
向您的应用程序发送另一个推送通知。UIApplication.shared.applicationIconBadgeNumber = 0
自行删除徽章计数。在 Appdelegate's didBecomeActive(:)
方法中添加这行代码。谢谢。
因为我一直在寻找 SwiftUI 解决方案,所以我想分享一下我的方法...
对于 Firebase 云消息传递,您正在 SwiftUI 代码中实现 AppDelegate(如 iOS 版 Firebase 网站上提到的),然后直接跳到 @main 结构...
就我而言...
@main
struct myApp: App {
// this is for your Firebase Cloud Messaging solution...
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
// to read the current phase of the app
@Environment(\.scenePhase) private var scenePhase
var body: some Scene {
WindowGroup {
ContentView()
.onChange(of: scenePhase) { phase in
// if app returns to the foreground do this:
if phase == .active {
// if your scenePhase hits the .active status (means it came from the
//background to the foreground OR is started) do the following..
UIApplication.shared.applicationIconBadgeNumber = 0
}
// if more phases are needed, add .inactive or .background
}
}
}
}