ios - Can Not Send Notification to iOS (iPhone) Firebase Cloud Messaging
问题描述
In Info.plist
, I have added FirebaseAppDelegateProxyEnabled
to NO (boolean)
, and capabilities for Background Modes --> Remote Notification
is ON and Push Notification
is ON too.
And on the iPhone device, has running with dialog to "allow notification"..
I have some codes like:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let gcmBroadcast = "gcm.broadcast"
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Messaging.messaging().delegate = self
// Use Firebase library to configure APIs
let firebasePListFileName = ControllerEnvironment.getGoogleService()
let firebaseOptions = FirebaseOptions(contentsOfFile: Bundle.main.path(forResource: firebasePListFileName, ofType: "plist")!)
FirebaseApp.configure(options: firebaseOptions!)
// firebase cloud messaging
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
}
// [START receive_message]
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// With swizzling disabled you must let Messaging know about the message, for Analytics
Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo[gcmBroadcast] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// With swizzling disabled you must let Messaging know about the message, for Analytics
Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo[gcmBroadcast] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
// [END receive_message]
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Unable to register for remote notifications: \(error.localizedDescription)")
}
// This function is added here only for debugging purposes, and can be removed if swizzling is enabled.
// If swizzling is disabled then this function must be implemented so that the APNs token can be paired to
// the FCM registration token.
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("APNs token retrieved: \(deviceToken)")
// With swizzling disabled you must set the APNs token here.
Messaging.messaging().apnsToken = deviceToken
}
}
// [START ios_10_message_handling]
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo[gcmBroadcast] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
// Change this to your preferred presentation option
completionHandler([])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
// Print message ID.
if let messageID = userInfo[gcmBroadcast] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
completionHandler()
}
}
// [END ios_10_message_handling]
extension AppDelegate : MessagingDelegate {
// [START refresh_token]
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
let dataDict:[String: String] = ["token": fcmToken]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
// TODO: If necessary send token to application server.
// Note: This callback is fired at each app startup and whenever a new token is generated.
}
// [END refresh_token]
// [START ios_10_data_message]
// Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground.
// To enable direct data messages, you can set Messaging.messaging().shouldEstablishDirectChannel to true.
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("Received data message: \(remoteMessage.appData)")
}
// [END ios_10_data_message]
}
and logs:
Firebase registration token: f6MUKT270iw:APA9.......Eg
APNs token retrieved: 32 bytes
I tried sending Push using firebase cloud messaging console as: User segment -> app
and got status completed in firebase list of messages.
But, actually my iPhone did not receive the notification.
How can I solve the issue?
解决方案
在 iOS 上,应用程序需要不在前台,否则开发人员会被告知推送通知,并且可以手动显示/处理它,否则 -> 确保应用程序不在前台
您还需要将 APNs 密钥或 APNs 证书添加到 Firebase。
推荐阅读
- javascript - 如何在 Node.js 中的同一进程内侦听同一地址上的不同 UDP 端口
- java - Java:根据特定属性从两个给定列表中创建包含所有不相交元素的列表的正确方法?
- ios - 解析实时查询:未在 iOS 客户端上接收事件。我们的 Parse 服务器托管在 Azure App Services 上
- c# - 如何检查任何 ContentDialog 是否打开?
- entity-framework - 使用参数列表查询多对多关系
- reactjs - Graphql TS1208“提供了isolateModules标志时无法编译命名空间
- javascript - 如何在 webpack 配置下拆分媒体查询?
- docker - 如何使用 docker-credential-pass 登录私有注册表?
- linux - 在 Docker 上以非 root 用户身份运行 JAR 的权限错误
- mysql - 无法删除最大用户连接数