Bat*_*Can 7 push-notification badge ios swift firebase-cloud-messaging
我正在使用FCM进行云消息传递.我想在后台和前台应用程序状态下从服务器收到推送通知时添加应用程序徽章.我错过了什么?主要问题是根据推送通知添加/更新/删除应用程序徽章,我可以接收和处理推送消息.我有3天这个问题.请帮帮我 !?*徽章编号根据内部内容而变化,例如,如果收到gmail应用程序的新电子邮件,徽章编号会更改为后台和前台应用程序状态中未尝试的邮件计数.
使用XCode 9.2,swift 3.2,iOS 11.6
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
var fcmtoken: String = ""
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()
if let token = Messaging.messaging().fcmToken {
fcmtoken = token
print("FCM token: \(fcmtoken)")
} else {
print("FCM token: \(fcmtoken) == no FCM token")
}
return true
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
Messaging.messaging().subscribe(toTopic: "all")
print("subscribed to all topic in didReceiveRegistrationToken")
// TODO: If necessary send token to application server.
// Note: This callback is fired at each app startup and whenever a new token is generated.
}
func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
Messaging.messaging().subscribe(toTopic: "all")
print("subscribed to all topic in notificationSettings")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
print("userInfo -- \(userInfo)")
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
print("user info in didReceive response -- \(userInfo)")
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("called to foreground app")
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().subscribe(toTopic: "all")
print("subscribed to all topic in didRegisterForRemoteNotificationsWithDeviceToken")
}
Run Code Online (Sandbox Code Playgroud)
有效负载就是您的内容:
我们刚才所做的大部分工作都会在本地通知中替换触发器。通知的内容可在有效负载中找到。回到测试平台,您会发现:
{"aps":{"alert":"Enter your message","badge":1,"sound":"default"}}
Run Code Online (Sandbox Code Playgroud)
理想情况下,您的JSON文件应如下所示。您只有4K的有效负载,因此在空间上浪费它已成问题。发送有效载荷时,请避免空格。但是,很难以这种方式阅读。看起来像这样更好:
{
"aps":{
"alert":"Enter your message",
"badge":1,
"sound":"default"
}
}
Run Code Online (Sandbox Code Playgroud)
aps是JSON字典,其中包含描述您的内容的条目。警报条目可以是此处的字符串,也可以是描述设备上显示的警报内容的字典。徽章给出了要在徽章图标上显示的数字。声音播放默认声音。您可以修改此有效负载以更改警报中显示的内容。由于警报既可以是字典,也可以是字符串,因此您可以为其添加更多内容。将有效负载更改为此:
{
"aps":{
"alert":{
"title":"Push Pizza Co.",
"body":"Your pizza is ready!"
},
"badge":42,
"sound":"default"
}
}
Run Code Online (Sandbox Code Playgroud)
这将添加标题和一条有关您的比萨准备就绪的消息。它还会将徽章更改为42
{"aps":{"alert":{"title":"Push Pizza Co.","body":"Your pizza is ready!"},"badge":42,"sound":"default"}}
Run Code Online (Sandbox Code Playgroud)
该通知将显示标题和正文。徽章显示为数字42。
但是,您也可以在应用程序处于活动状态时进行更改。通过注册UserNotificationType,您将需要用户的许可。获得许可后,可以将其更改为所需的任何数字。
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert |
UIUserNotificationType.Badge, categories: nil
))
application.applicationIconBadgeNumber = 5
Run Code Online (Sandbox Code Playgroud)
您也可以这样做:
let badgeCount: Int = 10
let application = UIApplication.shared
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
application.registerForRemoteNotifications()
application.applicationIconBadgeNumber = badgeCount
Run Code Online (Sandbox Code Playgroud)
结果:
参考:https : //makeapppie.com/2017/01/03/basic-push-notifications-in-ios-10-and-swift/
| 归档时间: |
|
| 查看次数: |
10431 次 |
| 最近记录: |