Chr*_*bst 2 iphone firebase-cloud-messaging swift4 ios11
我正在使用Firebase FCM从iOS设备发送推送通知。推送通知确实有效,直到昨天。
现在,当我发送推送通知时,一切都显示成功,但是设备上什么也没收到。
如果我直接通过curl请求发送,这是响应:
{"multicast_id":7815294000653973158,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1510474219556035%ca9ff0f8ca9ff0f8"}]}
Run Code Online (Sandbox Code Playgroud)
如果我是从Firebase控制台上的“通知”仪表板发送的,则表明已成功完成。
我已完成以下操作,但未成功:
使用Firebase(4.5.0)使用FirebaseAnalytics(4.0.4)使用FirebaseAuth(4.3.1)使用FirebaseCore(4.0.10)使用FirebaseFirestore(0.9.1)使用FirebaseInstanceID(2.0.5)使用FirebaseMessaging(2.0.6)
Messaging.messaging().shouldEstablishDirectChannel = true按Firebase通知进行设置在iOS 11中不起作用我的设置:我在重写init()方法的AppDelegate文件中调用FirebaseApp.configure():
override init() {
super.init()
FirebaseApp.configure()
}
Run Code Online (Sandbox Code Playgroud)
在AppDelegate中,didFinishLaunchingWithOptions:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// get push notification token id for user
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()
return true
}
Run Code Online (Sandbox Code Playgroud)
然后,将令牌保存在userdefaults中,并稍后保存到数据库中:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
if let refreshedToken = InstanceID.instanceID().token() {
defaults.set(refreshedToken, forKey: Constant.UserDefaults.token)
print("Token generated: ", refreshedToken)
} else {
print("Could not save token becuase error with instance id token")
}
}
Run Code Online (Sandbox Code Playgroud)
如上所述,这里我生成的令牌也用curl进行了测试,并且都显示成功。
请让我知道是否需要其他信息。
我的Android应用程序仍然可以像以前一样工作。我正在努力了解一天之内iOS应用程序可能会发生什么变化从而导致这种情况,或者一天之内我可能会遇到的问题:)
任何帮助将不胜感激。
好,修复它。我通过将Firebase的消息添加到我的Info.plist:FirebaseAppDelegateProxyEnabled:NO来禁用它
此外,我删除了所有Firebase消息传递委托方法。并在didRegisterForRemoteNotificationsWithDeviceToken中生成了我自己的APN令牌,并在生成令牌后在didRegisterForRemoteNotificationsWithDeviceToken方法中设置Messaging.messaging()。apnsToken = deviceToken。
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
if let refreshedToken = InstanceID.instanceID().token() {
defaults.set(refreshedToken, forKey: Constant.UserDefaults.token)
Messaging.messaging().apnsToken = deviceToken
print("Token generated: ", refreshedToken)
} else {
print("Could not save token becuase error with instance id token")
}
}
Run Code Online (Sandbox Code Playgroud)
以前,我使用了火力发电厂,看起来好像由于某种原因而停止工作。
由于这对我来说是一次艰难的经历,所以我想发布一下我现在建议为FCM启用iOS客户端的步骤:
然后在Firebase控制台的“设置”,“云消息传递”下,上传您的APN密钥,添加密钥ID和捆绑ID,不要上传任何p12证书。
FirebaseAppDelegateProxyEnabled:否
然后在didFinishLaunchingWithOptions方法的AppDelegate.swift文件中添加以下内容:
func application(_ application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptionsKey:Any]?)->布尔{
// [START set_messaging_delegate]
Messaging.messaging().delegate = self
// [END set_messaging_delegate]
// get push notification token id for user
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()
return true
Run Code Online (Sandbox Code Playgroud)
}
由于禁用了滑动,您需要注意生成APN令牌并将其分配给Firebase消息apnsToken。为此,请在AppDelegate.swift文件中使用以下方法:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
if let refreshedToken = InstanceID.instanceID().token() {
defaults.set(refreshedToken, forKey: Constant.UserDefaults.token)
Messaging.messaging().apnsToken = deviceToken
print("Token generated: ", refreshedToken)
} else {
print("Could not save token becuase error with instance id token")
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我将令牌保存在userdefaults中,以稍后保存到数据库中,请参见上文:
defaults.set(refreshedToken,forKey:Constant.UserDefaults.token)
您可以通过复制控制台中打印的令牌并使用FCM消息传递控制台来测试令牌是否正常工作。或通过如下所示在终端中使用curl请求。请注意,您必须用旧服务器密钥替换YOUR_LEGACY_SERVER_KEY,该旧服务器密钥可以在firebase控制台中的设置和云消息传递下找到,并用控制台中打印的令牌替换YOUR_TOKEN(由上面生成):
curl -X "POST" "https://fcm.googleapis.com/fcm/send" \
-H "Authorization: key=YOUR_LEGACY_SERVER_KEY” \
-H "Content-Type: application/json" \
-d $'{
"notification": {
"body": "Testing with direct FCM API",
"title": "Test Message",
"badge": "0",
"sound": "default"
},
"registration_ids": [YOUR_TOKEN]
}'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2433 次 |
| 最近记录: |