Hey guys my app works fine if I am using 3.2. On 3.2 I gather the [[FIRInstanceID instanceID] token] and I send a notification from the firebase console. Works great.
However on 3.3 I attempt the same and it gives me a token but never receives the message. I double checked to make sure notification permissions are accepted etc.. But no dice.
edit** upon inspection FirebaseInstanceID 1.0.7 seems to be the culprit here.
Anyone else seeing this problem? Code below
- (void)tokenRefreshNotification:(NSNotification *)notification
{
[[FIRInstanceID instanceID] token]
// Connect to FCM since connection may have failed when attempted before having a token.
[self connectToFcm];
}
- (void)connectToFcm
{
[[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) {
if (error != nil)
{
NSLog(#"Unable to connect to FCM. %#", error);
}
else
{
NSLog(#"Connected to FCM.");
}
}];
}
Related
I've been trying to setup push notifications on iOS 13.4 for a week now and I've been getting nowhere. I setup a blank application with just the minimal amount of code and it still doesn't seem to work. I get the log that the user has granted permissions but I do not receive the APNS token, I do not receive an error and method doesn't even get called. What might I be doing wrong?
I have setup the app id to have push notifications on the certificates and profiles page. I have also setup capabilities to have push notifications, remote notifications and background fetch.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert |
UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter]
requestAuthorizationWithOptions:authOptions
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if(granted){
NSLog(#"PERMISSION GRANTED");
}
else{
NSLog(#"PERMISSION GRANTED");
}
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] registerForRemoteNotifications];
});
}];
// Override point for customization after application launch.
return YES;
}
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
NSLog(#"DEVICE DID REGISTER:");
NSLog(#"DEVICE TOKEN: %s", deviceToken);
}
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {
NSLog(#"DEVICE DID FAIL TO REGISTER:");
}
First check in apple developer account the certificates you have created having your device UDID or not. Also take care of that if you are testing it on simulator than it will not work. You can only check notifications on real devices.
Secondly APNS works when your app is in background mode to fetch notifications in foreground mode we need to make some settings in the app or else need to use third party libraries.
If need to check the complete scenario you can check the link attached below:-
https://www.appcoda.com/push-notification-ios/
I am Using Firebase phone number login authentication All things are perfect
1) Provisioning profile
2) certificate
3) Signing methods Enable
4) Project setting with .12 file
5) everything should perfect
Issue
When I send mobile number for the OTP using thins method using this code
NSString *phoneNumber = #"+919428936703";
[[FIRPhoneAuthProvider provider]
verifyPhoneNumber:phoneNumber
completion:^(NSString * verificationID,
NSError * error) {
NSLog(#"VARIFICATION CODE %#", verificationID);
NSLog(#"Error %#", error);
if (!error){
}else{
}
}];
Also get call methods
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Pass device token to auth.
[[FIRAuth auth] setAPNSToken:deviceToken type:FIRAuthAPNSTokenTypeSandbox];
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)notification
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(#"DATA OF AUTH %#", [FIRAuth auth]);
if ([[FIRAuth auth] canHandleNotification:notification]) {
NSLog(#"Handle by Firebase ");
completionHandler(UIBackgroundFetchResultNoData);
return;
}else{
NSLog(#"NOT HANDLE BY FIREBASE %#", notification);
}
}
but then after getting a crash with this error log
-[__NSCFString setFir_authPhoneNumber:]: unrecognized selector sent to instance 0x166388b0
It looks like you're not linking your app with -ObjC linker flag, which is part of the instructions for Integrate without CocoaPods.
setFir_authPhoneNumber: is implemented as a category, thus -ObjC linker flag must be used or the compiled .o from the library won't be linked into your app binary.
I have integrated Firebase cloud messaging in my iOS application without cocoa pods. Firebase analytics is working fine. But FCM token is received on simulator but not real device. On real device I keep getting error
Failed to fetch default token Error Domain=com.firebase.iid Code=501
"(null)"
I have uploaded the .p12 certificates for development and prod on Firebase
I have checked the bundle ID for my app and that on Firebase console
I have Push notification enable on my App ID.
Here is my code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[FIRApp configure];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(tokenRefreshNotification:) name:kFIRInstanceIDTokenRefreshNotification object:nil];
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
// For iOS 10 display notification (sent via APNS)
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
// For iOS 10 data message (sent via FCM)
[FIRMessaging messaging].remoteMessageDelegate = self;
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeSandbox];
}
- (void)tokenRefreshNotification:(NSNotification *)notification {
// Note that this callback will be fired everytime a new token is generated, including the first
// time. So if you need to retrieve the token as soon as it is available this is where that
// should be done.
NSString *refreshedToken = [[FIRInstanceID instanceID] token];
NSLog(#"InstanceID token: %#", refreshedToken);
// Connect to FCM since connection may have failed when attempted before having a token.
[self connectToFcm];
// TODO: If necessary send token to application server.
}
- (void)connectToFcm {
// Won't connect since there is no token
if (![[FIRInstanceID instanceID] token]) {
return;
}
// Disconnect previous FCM connection if it exists.
[[FIRMessaging messaging] disconnect];
[[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(#"Unable to connect to FCM. %#", error);
} else {
NSLog(#"Connected to FCM. FCM token - %#", [[FIRInstanceID instanceID] token] );
}
}];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[FBSDKAppEvents activateApp];
[self connectToFcm];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
[[FIRMessaging messaging] disconnect];
NSLog(#"Disconnected from FCM");
}
Please help.
I got solution to my own problem. The device's date and time was incorrect. The second I changed it to current date & time, Firebase started giving me FCM token and got connected properly. I have checked the push notification as well from Firebase Notification Console. It is working in a way better than I imagined.
May be the issue is due the version problem.Actually me also faced the same problem.I got the FCM token on simulator but not on the device.The reason is due to registering the User notifications setting on didFinishLaunchingWithOptions..... We have to check the version on both device and simulator... If both are not same..please check the " [[UIApplication sharedApplication] registerUserNotificationSettings:settings] "
conditions for the current device version... Bcz I restricted this for >= 10 versions in didFinishLaunchingWithOptions....
I am actually trying to integrate FCM push notification feature in iOS. I created an account in the Firebase console and I am able to send notifications to the app, but problem is when I try from InvalidParameterException, I registered application in Amazon SWS, there I am getting an exception while sending notification to app.
Crash log:
Caused by: com.amazonaws.services.sns.model.InvalidParameterException: Invalid parameter: Token Reason: iOS device tokens must be 64 hexadecimal characters (Service: AmazonSNS; Status Code: 400; Error Code: InvalidParameter; Request ID: 6df7d1e9-0455-5d27-a2e7-818bd3b434f0)
integration code:
token:
eZkS-MKTCUo:APA91bFlaaure5iHcHFUBjdw_c-yyWmQp4H3OGeeb-mh6QIHSLyzehoKrM4_dbtLm3wPBrMi-vTxsHZ44pc8VNhLsOZ3H7sdhoa2WpCjpcyEZhRiJQiBiCQzGFmK7O5chAkf0pnkn2rj
code:
- (void)application:(UIApplication )application didReceiveRemoteNotification:(NSDictionary )userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// 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
// Print message ID.
NSLog(#"Message ID: %#", userInfo[#"gcm.message_id"]);
// Print full message.
NSLog(#"%#", userInfo);
}
// [END receive_message]
// [START ios_10_message_handling]
// Receive displayed notifications for iOS 10 devices.
// [END ios_10_message_handling]
// [START refresh_token]
- (void)tokenRefreshNotification:(NSNotification *)notification {
// Note that this callback will be fired everytime a new token is generated, including the first
// time. So if you need to retrieve the token as soon as it is available this is where that
// should be done.
NSString *refreshedToken = [[FIRInstanceID instanceID] token];
NSLog(#"InstanceID token: %#", refreshedToken);
// Connect to FCM since connection may have failed when attempted before having a token.
[self connectToFcm];
// TODO: If necessary send token to application server.
}
// [END refresh_token]
// [START connect_to_fcm]
- (void)connectToFcm {
[[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(#"Unable to connect to FCM. %#", error);
} else {
NSLog(#"Connected to FCM.");
}
}];
}
Integrate Firebase cloud messaging in my application. Successfully install pod and get all the framework. I want to get the refreshedToken for my device and send the notification and receive in my device, and want to track all the button click in my app.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
// iOS 7.1 or earlier
UIRemoteNotificationType allNotificationTypes =
(UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge);
[application registerForRemoteNotificationTypes:allNotificationTypes];
} else {
// iOS 8 or later
// [END_EXCLUDE]
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
// [START configure_firebase]
[FIRApp configure];
// [END configure_firebase]
// Add observer for InstanceID token refresh callback.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(tokenRefreshNotification:)
name:kFIRInstanceIDTokenRefreshNotification object:nil];
return YES;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// 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
// Print message ID.
NSLog(#"Message ID: %#", userInfo[#"gcm.message_id"]);
// Pring full message.
NSLog(#"%#", userInfo);
}
- (void)connectToFcm {
[[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(#"Unable to connect to FCM. %#", error);
} else {
NSLog(#"Connected to FCM.");
}
}];
}
- (void)tokenRefreshNotification:(NSNotification *)notification {
// Note that this callback will be fired everytime a new token is generated, including the first
// time. So if you need to retrieve the token as soon as it is available this is where that
// should be done.
NSString *refreshedToken = [[FIRInstanceID instanceID] token];
NSLog(#"InstanceID token: %#", refreshedToken);
// Connect to FCM since connection may have failed when attempted before having a token.
[self connectToFcm];
// TODO: If necessary send token to appliation server.
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[FIRMessaging messaging] disconnect];
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[self connectToFcm];
}
I'm using this code in app delegate. Got these waring in my code. How can I fix this issues. new for development help me.
WARNING: Firebase Analytics App Delegate Proxy is disabled. To log deep link campaigns manually, call the methods in FIRAnalytics+AppDelegate.h.
FCMAPP[496:56073] Configuring the default app.
Failed to fetch APNS token Error Domain=com.firebase.iid Code=1001 "(null)"
FIRMessaging library version 1.1.0
Firebase Analytics v.3200000 started
To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled
FIRMessaging registration is not ready with auth credentials
FCMAPP[496:56073] Unable to connect to FCM. Error Domain=com.google.fcm Code=501 "(null)"
Use the following Swift code.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
if #available(iOS 10.0, *)
{
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.currentNotificationCenter().delegate = self
UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert]) { (granted, error) in
if granted
{
//self.registerCategory()
}
}
// For iOS 10 data message (sent via FCM)
FIRMessaging.messaging().remoteMessageDelegate = self
}
else
{
let settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: [.Alert,.Badge,.Sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
//Configuring Firebase
FIRApp.configure()
// Add observer for InstanceID token refresh callback.
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.tokenRefreshNotification), name: kFIRInstanceIDTokenRefreshNotification, object: nil)
return true
}
//Receive Remote Notification on Background
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void)
{
FIRMessaging.messaging().appDidReceiveMessage(userInfo)
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)
{
FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Sandbox)
FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Prod)
}
func tokenRefreshNotification(notification: NSNotification)
{
if let refreshedToken = FIRInstanceID.instanceID().token()
{
print("InstanceID token: \(refreshedToken)")
}
// Connect to FCM since connection may have failed when attempted before having a token.
connectToFcm()
}
func connectToFcm()
{
FIRMessaging.messaging().connectWithCompletion { (error) in
if (error != nil)
{
print("Unable to connect with FCM. \(error)")
}
else
{
print("Connected to FCM.")
}
}
}
func applicationDidBecomeActive(application: UIApplication)
{
connectToFcm()
}
Have you set up the application correctly on the Firebase dashboard?
I have encountered this error before, as the Cloud Messaging settings aren't too obvious on the Firebase console.
Open the Firebase console. Click the cog next to your application name and go on "Project Settings".
Select the cloud messaging tab. You'll need to add your iOS APNS certificates here to successfully send push messages (One for development, one for production).
I was getting this error as I hadn't correctly uploaded my apps certificates.
Update:
Another reason for this error that i've encountered this error is that the Firebase SDK sometimes can't obtain the APNS token from apple itself. To resolve this, i've obtained the token manually:
First register for remote notifications:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
// Register for remote notifications.
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
When the APNS token is returned, manually set it on the Firebase instance:
- (void)application:(UIApplication *)app
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
NSLog(#"Registered for notification device token: %#", devToken);
[[FIRInstanceID instanceID] setAPNSToken:devToken type:FIRInstanceIDAPNSTokenTypeUnknown];
[self connectToFcm];
const void *devTokenBytes = [devToken bytes];
}
Should then successfully be able to connect to Firebase cloud messaging:
- (void)connectToFcm {
NSLog(#"Connecting to FCM...");
[[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(#"Unable to connect to FCM. %#", error);
} else {
NSLog(#"Connected to FCM.");
}
}];
}
If you cannot obtain the APNS token this way, there is something wrong with your push notification setup on the apple member center. Have you generated a developer/release APNS certificate and added them to your provision profile?