swift - 没有在我的 iPhone 上收到推送通知,但 Firebase Functions 说它已成功发送?
问题描述
我希望 Firebase 在用户收到新消息时发送推送通知,它曾经可以工作,但由于 FCM 令牌的一些错误没有保存给新用户,我不得不更改一些代码。但是,现在它将正确的 FCM 令牌保存给已注册的新用户。
但是,当用户现在收到一条消息时,没有显示任何通知......我检查了我在 firebase 函数上的日志,它说消息已成功传递(因此我认为这是我的代码中的错误)。
这是我在 AppDelegate 中获取用户 FCM 令牌的方式:
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
{
InstanceID.instanceID().instanceID { (result, error) in
if let error = error {
print("Error fetching remote instange ID: \(error)")
} else if let result = result {
print("Remote instance ID token: \(result.token)")
self.tokenOfUser = result.token
}
}
}
这就是我在用户注册时在 SignUpViewController 中检索令牌的方式:
var deviceToken : String?
var delegate = UIApplication.shared.delegate as! AppDelegate
self.deviceToken = delegate.tokenOfUser
//and in saving the user profile i add this:
["fcmToken": deviceToken]
这是我在 FeedViewController 中的通知代码
这是用户注册后的第一个视图控制器。
override func viewDidLoad() {
super.viewDidLoad()
registerForRemoteNotifications()
}
func registerForRemoteNotifications() {
Messaging.messaging().delegate = self
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)
UIApplication.shared.registerUserNotificationSettings(settings)
}
UIApplication.shared.registerForRemoteNotifications()
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken
fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
//UserDefaults.standard.set(fcmToken, forKey: "fcmToken")
}
我知道这些功能没有问题,因为它曾经完美地传递通知(所以这些功能以前可以工作)
我希望 Cloud Functions 向我的设备发送推送通知(在存储在我的数据库中的 FCM 令牌的帮助下)。
解决方案
发现了问题。我将 FirebaseAppDelegateProxyEnabled 设置为 NO。
我将其更改FirebaseAppDelegateProxyEnabled
为 YES ,现在它工作正常!
推荐阅读
- c# - 在控制台应用程序中调用 C# Web API 服务
- git - 如何修复包含大量悬空对象的 git 存储库,导致 git gc --prune=now 失败并出现致命:坏对象:
- angular - 如何使用 LazyLoading 在 Angular 路由中对一个模块中的不同路径进行分组?
- amazon-web-services - 通过 cloudformation 自动设置 AWS SSO 预置权限
- c# - ScrollViewer 显示但无法滚动,并且内容离开窗口到底部
- php - mysqli_stmt 对象未完全初始化
- python - 为什么插入方法会影响两个列表?
- javascript - 通过条带结帐会话传递参数
- asp.net - ASP.NET Core 5 MVC - Google Auth 不起作用 - 执行登录后没有任何反应
- angular - 部署后我无法加载角度应用程序