I use Firebase to send Notification with my iOS app, I have followed all step in documentation :
Apple Developer Account Configuration
Generating a CSR file
Uploading CSF file
Preparing the APNs Certificate
Configuring Firebase for Push Notifications
Building the Firebase Notification in my app
When I try to send a Notification there isn't no problem found in Firebase, for one particular user or for all iOS device. But none of my device (real device obviously) receive notification.
I use the good Bundle, I enable notification in my application and there is the code in my AppDelegate :
import UIKit
import UserNotifications
import Firebase
import FirebaseInstanceID
import FirebaseMessaging
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UINavigationBar.appearance().barTintColor = Design.blue_es
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
UINavigationBar.appearance().tintColor = UIColor.white
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 })
// For iOS 10 data message (sent via FCM
Messaging.messaging().remoteMessageDelegate = self
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
FirebaseApp.configure()
return true
}
func application(received remoteMessage: MessagingRemoteMessage) {
print(remoteMessage.appData)
}
// Called when APNs failed to register the device for push notifications
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
// Print the error to console (you should alert the user that registration failed)
print("APNs registration failed: \(error)")
}
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
}
}
IN Swift 3
Configuring Your Apple Developer Account
Generating a CSR file
Uploading Your CSR File
Preparing the APNs Certificate
Configuring Firebase for Push Notifications
Building the Firebase Notification App
Installing the Firebase SDK Using CocoaPods
Adding GoogleService-Info.plist
Enabling Push Notifications
Initializing Push Notifications
AppDelegate of your Project
import UIKit
import SVProgressHUD
import UserNotifications
import Firebase
import FirebaseInstanceID
import FirebaseMessaging
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate, MessagingDelegate {
var window: UIWindow?
static var appDelegate:AppDelegate!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
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 })
// For iOS 10 data message (sent via FCM
Messaging.messaging().remoteMessageDelegate = self
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
FirebaseApp.configure()
return true
}
func application(received remoteMessage: MessagingRemoteMessage) {
print(remoteMessage.appData)
}
func application(application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
Messaging.messaging().apnsToken = deviceToken as Data
}
}
I think first configure for firebase
FirebaseApp.configure()
after that you should call registerForRemoteNotifications()
Related
I am developing a fluter application where I have set up the APN key and uploaded the certificate to the firebase console and following is the code in my AppDelegate.swift:
import UIKit
import Flutter
import FirebaseMessaging
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: 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)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
super.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
}
}
When I am sending the notification from the firebase console, it displays in my android application but it does not show in my iOS application. Which step am I missing here?
The firebase FCM is not working for me in flutter 2.2.2, am trying to implement push notification in my ios app and am getting this error :
8.3.0 - [Firebase/Messaging][I-FCM002022] APNS device token not set before retrieving FCM Token for Sender ID '*************'.
Notifications to this FCM Token will not be delivered over APNS.Be
sure to re-retrieve the FCM token once the APNS device token is set.
It’s been one month since this problem occurred.
I followed the step in this article and article
the push notifications works fine in Android the problem is with the iOS
this is my AppDelegate.swift
import UIKit
import Flutter
import Firebase
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
// Use Firebase library to configure APIs
FirebaseApp.configure()
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()
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
you have to add
Messaging.messaging().apnsToken = deviceToken
before
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
I've programmed a new App in Swift including Firebase.
When I run the App via Simulator, the Cloud Messaging (Push Notifications) works. But as I submitted it to TestFlight, no Message has appeared.
I've created all Certificates (for Development and Production) to send Push Notifications.
I think theres something with the token but I have no clue what :/ Can someone help me?
Here is my Code (AppDelegate.swift)
import UIKit
import CoreData
import Firebase
import UserNotifications
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate{
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey:Any]?) -> Bool {
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 })
// For iOS 10 data message (sent via FCM
Messaging.messaging().remoteMessageDelegate = self
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
FirebaseApp.configure()
return true
}
func application(received remoteMessage: MessagingRemoteMessage) {
print(remoteMessage.appData)
}
I implement Firebase push notifications in my app. I can get the alert message that I sent from Firebase console, but it's just the alert, no sound, no badge.
So I think I have to handle the badge and the sound by myself using import UserNotifications, so how do I do that?
Here is the code I use to implement push notifications:
import UIKit
import Firebase
import UserNotifications
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
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()
Messaging.messaging().delegate = self
let token = Messaging.messaging().fcmToken
print("FCM token: \(token ?? "")")
return true
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String){
print("Firebase registration token: \(fcmToken)")
}
}
I am facing this strange issue in my application. I am trying to integrate push notification in my application using firebase. I have included Firebase SDK in the project using the downloaded SDK(not using pod). I have included the following frameworks from the downloaded zip file in the application:
In AppDelegate
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
return true
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()
}
// This is called and the access token is received.
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
var token = ""
for i in 0..<deviceToken.count {
token = token + String(format: "%02.2hhx", arguments: [deviceToken[i]])
}
print(token)
Messaging.messaging().setAPNSToken(deviceToken, type: .sandbox)
}
// This is not getting called
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
Messaging.messaging().subscribe(toTopic: "ios_topic")
}
The access token I get in didRegisterForRemoteNotificationsWithDeviceToken is valid and I successfully sent push to the device using that. But the didRefreshRegistrationToken of FireBase is not getting called. I have added -ObjC in linkerflags and FirebaseAppDelegateProxyEnabled as NO in info.plist. I have added the GoogleService-Info.plist file in the project root, and the project is configured correctly in FireBase console.
What is the possible issue here and how to solve it?
That was the silliest of mistakes while following the tutorials. Just needed to set the delegate of Messaging.messaging() as AppDelegate. Add the following code right under FirebaseApp.configure() and the issue is solved.
FirebaseApp.configure()
Messaging.messaging().delegate = self