i had implement Firebase with FirebaseAuth/FCM etc and did sent notification successfully through Firebase Console.
However i would need to push the notification from m
First of all, you should import all required libs
import Firebase
import UserNotifications
later in AppDelegate.swift call next function
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
InstanceID.instanceID().instanceID { (result, error) in
if let error = error {
print("Error fetching remote instance ID: \(error)")
} else if let result = result {
print("Remote instance ID token: \(result.token)")
}
}
}
confirm to MessagingDelegate protocol.
then you can add below delegate method and get the Firebase Token. (documentation https://firebase.google.com/docs/cloud-messaging/ios/client )
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
// send to remote server
InstanceID.instanceID().instanceID { result, error in
if let error = error {
print("Error fetching remote instance ID: \(error)")
} else if let result = result {
print("Remote instance ID token: \(result.token)")
}
}
}
This question is old but still if someone want to use in Objective C.
Latest Firebase: 6.27.0
In iOS Objective C we can use like this
[[FIRInstanceID instanceID] instanceIDWithHandler:^(FIRInstanceIDResult * _Nullable result,
NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Error : %@", error);
} else {
token = result.token;
}
NSLog(@"Token %@", result.token);
}];
and to get Instance Id:
[[FIRInstanceID instanceID] getIDWithHandler:^(NSString *identity, NSError *error) {
if (error != nil) {
NSLog(@"Error : %@", error);
} else {
NSLog(@"instance ID: %@", identity);
}
NSLog(@"IID %@", identity);
}];
static var FirebaseToken : String? {
return InstanceID.instanceID().token()
}
Swift 4
Courtesy of: https://stackoverflow.com/a/50945350/1014164
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)")
}
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
let deviceTokenString = deviceToken.reduce("") { $0 + String(format: "%02X", $1) }
print("APNs device token: \(deviceTokenString)"
}