javascript - iOS 为推送通知声音 FCM 启用声音
问题描述
我正在尝试为我的 Firebase 推送通知启用声音,但我不确定 App Delegate 中是否有我需要实现的代码,或者我的 index.js 中的代码是否错误。
我认为我需要在 AppDelegate 中导入与声音相关的内容,但我发现的所有实现推送通知的指南都只有基本代码,其中 [options] 包含唯一与通知声音相关的内容。index.js 代码:
var notification = {
notification: {
title: conversation.conversationName,
body: user.username + ': ' + message.text,
sound: 'default'
},
topic: topic
}
应用程序委托代码:在 didFinishLaunchingWithOptions 中调用的函数。
import UIKit
import Firebase
import UserNotifications
private func attemptRegisterForNotifications(application: UIApplication) {
Messaging.messaging().delegate = self
UNUserNotificationCenter.current().delegate = self
let options: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
if settings.authorizationStatus == .authorized {
// Push notifications already authorized, do nothing
print ("push notifications authorized")
} else if settings.authorizationStatus == .notDetermined {
// User hasn't specified notification status
UNUserNotificationCenter.current().requestAuthorization(options: options, completionHandler: { (granted, error) in
if let error = error {
print ("Failed to request authorization:", error)
return
}
guard granted else {return}
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
})
} else if settings.authorizationStatus == .denied {
// User has denied notifications
UNUserNotificationCenter.current().requestAuthorization(options: options, completionHandler: { (granted, error) in
if let error = error {
print ("Failed to request authorization:", error)
return
}
let alertController = UIAlertController(title: "Enable Push Notifications", message: "Enable push notifications for optimal chat experience", preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
})
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alertController.addAction(cancelAction)
alertController.addAction(settingsAction)
alertController.preferredAction = settingsAction
DispatchQueue.main.async {
self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
}
})
}
}
}
解决方案
推荐阅读
- ionic-framework - 我需要重新开始吗?- 离子 4
- r - 您可以在 RMarkdown 中左对齐或加粗 Kable 的表格/图形标题吗?
- r - 如何选择任何年份并且只显示所选年份的行
- laravel - Laravel 5.8 安装错误在 database.php 第 58 行:未定义的类常量 'MYSQL_ATTR_SSL_CA'
- python - 在 virtualenv 中运行测试时出现 Django MySQL 错误
- python - 使用子查询对 SQLAlchemy (flask-sqlalchemy) 进行 SQL 查询
- angular - 在 Angular 7 中将 html 转换为 pdf 的问题(使用 ASP.Net Core)
- python - 正则表达式匹配 mRNA 序列
- python - 找到最小营业额账户
- ruby-on-rails - 在rails中安装milia的问题