I'm having a big problem:
When I install the app and run the app for the first time, both the APNS and FCM notifications work ok. I can send Push Notifications via FCM and they're received via FCM and APNS with no problem.
When the FCM token is updated though, I stop receiving Notifications through the method didReceiveRemoteNotification at all, and am only able to receive messages through the (received remoteMessage: MessagingRemoteMessage) - that said, the Notification dictionary of my push isn't read as a APNS.
I haven't disabled FCM's swizzling, here's the code I use for setting up Push Notifications:
func setupUserNotifications(application: UIApplication) {
if #available(iOS 10.0, *) {
let authOptions : UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions, completionHandler: {_,_ in })
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
// For iOS 10 data message (sent via FCM)
Messaging.messaging().delegate = self
} else {
// Fallback on earlier versions
let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
FirebaseApp.configure()
if let token = Messaging.messaging().fcmToken() {
connectToFcm(token)
}
}
Same situation here. Push notifications work perfect until firebase token is refreshed. After token refresh device stops receiving notifications(no matter using server API, or firebase console).
Hmm, when you say:
When the FCM token is updated though, I stop receiving Notifications
Are you triggering an FCM token update, somehow? How can I replicate this issue?
Yes i'm triggering FCM token update by deleting FIRInstanceID.
[[FIRInstanceID instanceID]deleteIDWithHandler:^(NSError * _Nullable error) {
NSLog(@"%@", error.description);
}];
What is interesting same technique is used on Android, and notifications do not stop to work.
Exactly. I'm forcing the FCM token update through that very same method - though on Swift.
That said, the very same problem eventually happens naturally if I don't force the FCM Token update.
Thanks folks, I'm going to look into this. I'm just curious: Why are you deleting the ID? Is this a common thing? Trying to understand the scenario. Thanks!
I'm deleting the ID as a means of testing something that eventually will happen naturally.
Same issue here. I am not deleting the ID but I distribute new versions of the app in Crashlytics to internal testers. When testers install a newer version of the app, the FCM token is updated. From this point no APNS notification is received anymore, but fcm messaging are received correctly.
I can confirm that the new FCM token is correctly updated on our backend side, and push notifications permissions are granted on client side.
I have exactly the same issue.. Started happening after updating Firebase to version 4 and using the new p8 file.
What's maybe of interest is, after the token is changed, I can still send push notifications to the device with the previous old token, even though Firebase is reporting a completely new token. Sending to the new token gives me a success response on the sender side, but the message is never received.
Hi, we have the same issue and for us it is quite important.
Our App has a function to reset all private data. This is where we delete the token. If we do not do this, we fear that users will delete their private data, give their phone to someone else, who then gets messages meant for the prior owner. We believe that this presents a significant legal and reputation risk here in the EU. We are currently looking into workarounds, which might delay us launching, so it would be much appreciated if this could be fixed soon.
We have the same problem.
After reset we need to delete all user data as well as FCM token. If FIRInstanceID.instanceID().delete() runs into error, user will never get any notifications.
@rsattar We call FIRInstanceID.instanceID().delete(), because we have to make sure that after app reset, new user will get a new token.
I have a similar problem. After deleting the instance ID, a new token is not generated, the library always returns the same Token.
However, if you kill the App and open it again, a new token is generated normally and the messages come back to work.
Hmm, I'm unable to reproduce this. If you're not on the latest Firebase InstanceID SDK, please do so. There have been several internal changes recently that could have addressed this issue.
This is what I'm doing:
FIRInstanceID *iid = [FIRInstanceID instanceID];
[iid deleteIDWithHandler:^(NSError *error) {
// Force a token fetch, if one hasn't started
NSString *token = iid.token;
}];
In this call, iid.token returns nil, because the tokens associated with that appID have been deleted/invalidated. But a new token will be fetched. When I test that token, it seems to deliver notifications over APNs in my test app.
Questions:
deleteIDWithHandler?Hey, I know this has been closed. But I have to make sure something. I have the similar issue here. my code is like that:
- (AnyPromise *)resetFCMToken
{
return [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) {
[[FIRInstanceID instanceID] deleteTokenWithAuthorizedEntity:[self fcmAuthorizedEntity] scope:kFIRInstanceIDScopeFirebaseMessaging handler:^(NSError * _Nullable error) {
DLog(LOG_APNS, @"Delete FCM Token Error: %@", error);
if (error) {
resolve(error);
} else {
resolve(nil);
}
}];
}];
}
- (AnyPromise *)refreshFCMToken
{
return [self resetFCMToken].then(^{
[[FIRInstanceID instanceID] tokenWithAuthorizedEntity:[self fcmAuthorizedEntity] scope:kFIRInstanceIDScopeFirebaseMessaging options:@{@"apns_token":[FIRMessaging messaging].APNSToken} handler:^(NSString * _Nullable token, NSError * _Nullable error) {
DLog(LOG_APNS, @"Refresh FCM Token, error : %@, token : %@", error, token);
}];
});
}
I call deleteTokenWithAuthorizedEntity before tokenWithAuthorizedEntity, and I get an error in tokenWithAuthorizedEntity handle block did I do someting wrong? or how could we do if we need to delete the old FCM_token and fetch a new one?
Error : token Error Domain=com.firebase.iid Code=1002 "(null)". I was unable to find the error code 1002 through the doc
That error means you're missing the APNs server type in your options dictionary.
This is an error that's no longer referenced in the most recent version of InstanceID, which just provides a default for you now if it's missing.
Hey, thanks for your heads-up ;) But according to this docs, they didn't mention APNs server type, and the apns_token is only required
Docs: https://firebase.google.com/docs/reference/swift/firebaseinstanceid/api/reference/Classes/InstanceID
So I added a dictionary like this options:@{@"apns_token":[FIRMessaging messaging].APNSToken}
I'm not sure how to fix it. Should I add something like this:
@{@"apns_token":[FIRMessaging messaging].APNSToken,
@"apns_type":???,
}
Hey, I think I finally make it works. Just a record here.
I have to change the API that I used. Now, I use -deleteIDWithHandler instead of deleteTokenWithAuthorizedEntity and tokenWithAuthorizedEntityto reset fcmToken.
Looks that works fine for me but I'm still confused what this tokenWithAuthorizedEntity method meaning is since it can't update a new token?
Reset fcmToken Code: (You can ignore the AnyPromise that I used)
- (AnyPromise *)resetFCMToken
{
return [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) {
[[FIRInstanceID instanceID] deleteIDWithHandler:^(NSError * _Nullable error) {
if (error) {
resolve(error);
} else {
resolve(nil);
}
}];
}];
}
Then we need to register again and try to get the new fcmToken.
I set APNSToken to FIRMessaging [[FIRMessaging messaging] setAPNSToken:deviceToken type:FIRMessagingAPNSTokenTypeUnknown]; in -application-didRegisterForRemoteNotificationsWithDeviceToken method.
__That means the new fcmToken will be fetched after the next time app launched.__
Setup deviceToken:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[[FIRMessaging messaging] setAPNSToken:deviceToken type:FIRMessagingAPNSTokenTypeUnknown];
}
Fetch new token :
#pragma mark - FIRMessagingDelegate
- (void)messaging:(nonnull FIRMessaging *)messaging didRefreshRegistrationToken:(nonnull NSString *)fcmToken
{
// 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.
[self reportFCMTokenIfNeeded];
}
tokenWithAuthorizedEntity should fetch a new FCM token with the same instance ID for the specific server type (production, sandbox, etc). In the newest version of the SDK, the server type is no longer required (you should update your SDK version if you're not on the latest version).
Deleting the InstanceID instance forces a token refresh as well.
I am facing this issue not receiving notification after fcm token refresh.
I am using Firebase 5.15. I try in latest but didn't work.
Any one have solution ?
I'm facing this issue too, old token is still working though. Fix anyone ?
I have this issue as well - I get a new token but pushes don't work on update
Having a similar issue with APNS notifications not being delivered within the last few weeks out of the blue. Not sure if its due to a token refresh or what the deal is but we have come across devices where the FCM pushes to the result in a 200 OK and a success JSON payload but the notifications never make it to the user.
I found a good solution for React Native ios - should work with the native methods as well as it's the same sdk. Force delete the token on the device - for with react-native-firebase it's .deleteToken(). Also only rely off the onTokenRefresh callback as that's where you will always get the latest. Learn more about my issue and solution here: https://github.com/invertase/react-native-firebase/issues/2035#issuecomment-480925392
Same issue here. The strangest thing is, that sending a notification through APNs itself remakes the FCM notifications working properly for the FCM token coupled with the APNs token.
I have swizzling disabled an i set the APNs token properly to FIRMessaging (Objective-C). So the summary is:
and that without changing anything on the app. How can that be?
Same issue here. We have the following in our Podfile.lock
- FirebaseInstanceID (4.3.0):
- FirebaseCore (~> 6.6)
- FirebaseInstallations (~> 1.0)
- GoogleUtilities/Environment (~> 6.5)
- GoogleUtilities/UserDefaults (~> 6.5)
- FirebaseMessaging (4.2.0):
- FirebaseAnalyticsInterop (~> 1.5)
- FirebaseCore (~> 6.6)
- FirebaseInstanceID (~> 4.3)
- GoogleUtilities/AppDelegateSwizzler (~> 6.5)
- GoogleUtilities/Environment (~> 6.5)
- GoogleUtilities/Reachability (~> 6.5)
- GoogleUtilities/UserDefaults (~> 6.5)
- Protobuf (>= 3.9.2, ~> 3.9)
Seems like the issue only happens when we delete the token and then kill the app.
This is because FIRMessaging fails to get access to APNS token after the app is killed and then restarted.
Solution:
We need to somehow call [RCTSharedApplication() registerForRemoteNotifications] some time after the app restarts.
If you're using firebase-react-native, make sure to call firebase.messaging().ios.registerForRemoteNotifications() some time after app restarts
I've spent the whole day searching on why our notifications are not delivered after calling deleteIdWithHandler and it turns out the APN Token is not automatically passed when a new ID is requested/generated. Thus to solve this, thanks to all the comments here, we call UIApplication.shared.registerForRemoteNotifications() so the APN Token gets refreshed and Firebase notifications start working again. I think just by fetching and setting the APN token on the Messaging SDK would do the trick, but we decided to keep it clean and let the method swizzling take care of it.
I think a warning should be added somewhere in the documentation to let the users know they need to manually refresh the APN Token after deleting the instance ID.
Most helpful comment
I have exactly the same issue.. Started happening after updating Firebase to version 4 and using the new p8 file.
What's maybe of interest is, after the token is changed, I can still send push notifications to the device with the previous old token, even though Firebase is reporting a completely new token. Sending to the new token gives me a success response on the sender side, but the message is never received.