Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[🐛] The operation couldn’t be completed. No APNS token specified before fetching FCM Token #6893

Open
1 of 9 tasks
k2xl opened this issue Feb 7, 2023 · 85 comments
Open
1 of 9 tasks
Labels
Help: Needs Triage Issue needs additional investigation/triaging. Impact: Bug New bug report

Comments

@k2xl
Copy link

k2xl commented Feb 7, 2023

Issue

Trying to get token from user.

import messaging from '@react-native-firebase/messaging';
import { registerRootComponent } from 'expo';
import React, { useEffect } from 'react';
import {
  Text
} from 'react-native';

async function onAppBootstrap() {
  // Register the device with FCM
  console.log('A');
  await messaging().registerDeviceForRemoteMessages();
  console.log('B');
  // Get the token
  const token = await messaging().getToken();

  // Save the token
  console.log('TOKEN ' + token);
}

function App() {
  useEffect(() => {
    onAppBootstrap();
  }, []);

  return (
    <Text>Hi</Text>
  );
}

console.log('Registering root component');
registerRootComponent(App);
export default App;

When running this on iOS simulator, I get A on the console but it never reaches B. On further debugging just calling await messaging() seems to do enough to hang.

I see now after adding initializeApp() with credentials from firebase the following error

The operation couldn’t be completed. No APNS token specified before fetching FCM Token

Project Files

Javascript

Click To Expand

package.json:

"@react-native-firebase/app": "^17.0.0",
    "@react-native-firebase/messaging": "^17.0.0",

firebase.json for react-native-firebase v6:

# N/A

iOS

Click To Expand

ios/Podfile:

  • I'm not using Pods
  • I'm using Pods and my Podfile looks like:
# N/A

AppDelegate.m:

// N/A


Android

Click To Expand

Have you converted to AndroidX?

  • my application is an AndroidX application?
  • I am using android/gradle.settings jetifier=true for Android compatibility?
  • I am using the NPM package jetifier for react-native compatibility?

android/build.gradle:

// N/A

android/app/build.gradle:

// N/A

android/settings.gradle:

// N/A

MainApplication.java:

// N/A

AndroidManifest.xml:

<!-- N/A -->


Environment

Click To Expand

System:
OS: macOS 12.5
CPU: (10) arm64 Apple M1 Pro
Memory: 227.88 MB / 16.00 GB
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 19.5.0 - /opt/homebrew/bin/node
Yarn: Not Found
npm: 9.3.1 - /opt/homebrew/bin/npm
Watchman: Not Found
Managers:
CocoaPods: 1.11.3 - /opt/homebrew/bin/pod
SDKs:
iOS SDK:
Platforms: DriverKit 22.2, iOS 16.2, macOS 13.1, tvOS 16.1, watchOS 9.1
Android SDK: Not Found
IDEs:
Android Studio: 2022.1 AI-221.6008.13.2211.9477386
Xcode: 14.2/14C18 - /usr/bin/xcodebuild
Languages:
Java: 17.0.6 - /usr/bin/javac
npmPackages:
@react-native-community/cli: Not Found
react: 18.1.0 => 18.1.0
react-native: 0.70.5 => 0.70.5
react-native-macos: Not Found
npmGlobalPackages:
react-native: Not Found

 OUTPUT GOES HERE
  • Platform that you're experiencing the issue on:
    • iOS
    • Android
    • [x ] iOS but have not tested behavior on Android
    • Android but have not tested behavior on iOS
    • Both
  • react-native-firebase version you're using that has this issue:
    • `"@react-native-firebase/app": "^17.0.0",
  • "@react-native-firebase/messaging": "^17.0.0",`
    
  • Firebase module(s) you're using that has the issue:
    • e.g. Instance ID
  • Are you using TypeScript?
    • Y


@k2xl k2xl added Help: Needs Triage Issue needs additional investigation/triaging. Impact: Bug New bug report labels Feb 7, 2023
@k2xl k2xl changed the title [🐛] await messaging().registerDeviceForRemoteMessages(); const token = await messaging().getToken(); [🐛] await messaging() hangs Feb 7, 2023
@k2xl
Copy link
Author

k2xl commented Feb 7, 2023

After adding initializeApp() with credentials from new firebase app it stopped hanging. However, now encountering:

The operation couldn’t be completed. No APNS token specified before fetching FCM Token

So trying to debug this now

@k2xl k2xl changed the title [🐛] await messaging() hangs [🐛] The operation couldn’t be completed. No APNS token specified before fetching FCM Token Feb 7, 2023
@mikehardy
Copy link
Collaborator

with the latest firebase-ios-sdk (v10.4.0+) an APNS token is strictly required as a prerequisite to get an FCM token
The SDKs try to handle this for you by swizzling in AppDelegate methods that register an APNS token if one comes, but if you disable swizzling, or you do not register for remote notifications prior to calling getToken, you'll never get one

  • do not disable swizzling
  • make sure you call registerForRemoteNotifications

@k2xl
Copy link
Author

k2xl commented Feb 7, 2023

Hi @mikehardy thanks for the response. I'm very confused. Apologies as I'm still relatively new to react-native. Note that I'm also using Expo.

In my example above, I am calling registerForRemoteNotifications before calling getToken.

@mikehardy
Copy link
Collaborator

I'm not sure why, but the FCM system does not have an APNS token set for your app before you request an FCM token.
There can be a few reasons why, as mentioned above it could be that swizzling is disabled in your AppDelegate such that the FCM callback which would normally listen for / register the APNS token with FCM when it comes in is not hooked up. It could be that you do not have remote messaging set up as an entitlement for your app. It could be lots of things.

One thing that might be uncomfortable to hear but is nevertheless important: others have this working. The code works in the module. Why is that important but maybe uncomfortable? Because it implies (though no, it does not quite prove...) that the problem is specific to your app and your configuration. You should triple check everything with the assumption the problem is in your app and no one will be able to help you find it - you will likely find the problem yourself in your app upon further careful inspection.

There is still of course the possibility the problem is in this module, but no one else is experiencing it so I am operating on the assumption the problem is project-specific

@k2xl
Copy link
Author

k2xl commented Feb 8, 2023

Thank you for your respectful response! Definitely appreciate this input.

I think part of the reason I raised this issue is that I did see some other threads for a few weeks ago related to this issue. So I thought just getting the minimal example to reproduce the issue could cause an issue - so in case anyone else encounters and has found a solution they may stumble upon this ticket.

The fact that this sounds like only affecting me indicates it is indeed a problem with my configuration - I guess the tough part I'll need to figure out is what is the issue with my configuration:).

Anyway best regards, and thanks again for the pointers

@mikehardy
Copy link
Collaborator

The new strict requirement for an APNS token prior to an FCM one is bound to turn up issues like this so I will be very curious to hear what you turn up. Right now the entitlements idea is my best one for you but maybe it's something else? There was a lot of traffic about this recently because they added this requirement first on the backend without warning which exposed lots of people getting FCM tokens with no corresponding APNS token and broke their apps.

Of course those non-APNS FCM tokens are useless, but breaking the misconfigured apps unexpectedly was bad and generated all the traffic. They relaxed the backend change but left the SDK change in for firebase-ios-sdk 10.4.0 release so it is now only seen by folks as they upgrade, which is you now. It's a valid change (the APNS token should be required...) but forces everyone to find their corner cases where it fails now. And here we are...

Anyway, definitely report back if you find anything, and I hope you do + quickly. Cheers

@nicolasburtey
Copy link

nicolasburtey commented Feb 8, 2023

also running into this issue today. not sure right now why that is.

it's not a minimal example, but issue can be seen by cloning this repo: https://github.com/GaloyMoney/galoy-mobile

@farojos
Copy link

farojos commented Feb 9, 2023

@k2xl I had the same issue minutes before.

I solved it by checking in signing & capabilities. Inside "Background modes", I selected "Background check" and "Remote notifications". I hope this helps you.

@nicolasburtey
Copy link

@k2xl I had the same issue minutes before.

I solved it by checking in signing & capabilities. Inside "Background modes", I selected "Background check" and "Remote notifications". I hope this helps you.

just tried that and had no effect on my end.

I realized I had not set use_frameworks! :linkage => :static in the Podfile even thought it was necessary from a recent update, but it doesn't seem to remove the error message.

@tautvilas
Copy link

I have been digging into this problem the whole evening. In my case the problem was that in firebase.json property registerDeviceForRemoteMessages was set to false. This caused getAPNSToken to return null and also triggered this error in case getToken was called.

I don't remember why did I set registerDeviceForRemoteMessages to false earlier before. But this still looks like firebase bug. Because I did call registerDeviceForRemoteMessages in my JS code, but for some reason that did not perform the registration fully. One interesting behaviour that I noticed was that APNSToken was not not null only in the case when app was launched first time after install (with registerDeviceForRemoteMessages=false setup) .

@nicolasburtey
Copy link

on my end, solving by doing a full re-install off the app. don't know how it got corrupted in the first place but anyway, no longer seen the error message.

@kesha-antonov
Copy link

I fixed this by changing firebase.json

From

{
  "react-native": {
    "analytics_auto_collection_enabled": false,
    "messaging_auto_init_enabled": false,
    "messaging_ios_auto_register_for_remote_messages": false
  }
}

To

{
  "react-native": {
    "analytics_auto_collection_enabled": false,
    "messaging_auto_init_enabled": false,
    "messaging_ios_auto_register_for_remote_messages": true
  }
}

And deleted call

await messaging().registerDeviceForRemoteMessages()

@k2xl
Copy link
Author

k2xl commented Feb 13, 2023

Thanks for the responses. I am on an expo managed app so didn't have a firebase.json file...

I tried adding a firebase.json to the root of the folder and removed the await messaging().registerDeviceForRemoteMessages() (since it was showing that it was unneeded).

However
await messaging().getToken();
hangs... and I still get The operation couldn’t be completed. No APNS token specified before fetching FCM Token

@Spxc
Copy link

Spxc commented Feb 16, 2023

Thanks for the responses. I am on an expo managed app so didn't have a firebase.json file...

I tried adding a firebase.json to the root of the folder and removed the await messaging().registerDeviceForRemoteMessages() (since it was showing that it was unneeded).

However await messaging().getToken(); hangs... and I still get The operation couldn’t be completed. No APNS token specified before fetching FCM Token

Hey, i just stumbled upon this issue, got a solution?
According to the docs setAPNS needs to be called, just not sure how or where

@mikehardy
Copy link
Collaborator

According to the docs setAPNS needs to be called,

I don't believe this statement is true? The docs should say (and I think they say?) that you must call setAPNS *if you have disabled method swizzling or for some other reason have disabled the automatic firebase APNS token handling on remote message registration

In almost all cases, certainly the "default integration" cases, if you call the API to register for remote notifications, and have @react-native-firebase/messaging installed in the default manner with correct entitlements on your iOS project to allow them, the SDK will request an APNS token from Apple and set it into the SDK for you fully automated.

You only want or need to use setAPNS token if you really want to manage APNS tokens yourself (for automated testing on non-Apple-Silicon emulators, perhaps, or if you have some brownfield app where you manage APNS tokens yourself, etc)

@mikehardy
Copy link
Collaborator

We appear to have an issue in the reference API docs where line breaks in our method docs stop the rest of the doc from rendering but the whole doc is as such:

/**
* On iOS, This method is used to set the APNs Token received by the application delegate.
* Note that the token is expected to be a hexadecimal string, as it is an NSData type in
* the underlying native firebase SDK, and raw data may only be passed as a string if it is
* hex encoded. Calling code is responsible for correct encoding, you should verify by comparing
* the results of `getAPNSToken()` with your token parameter to make sure they are equivalent
*
* Messaging uses method swizzling to ensure that the APNs token is set automatically.
* However, if you have disabled swizzling by setting FirebaseAppDelegateProxyEnabled to NO
* in your app’s Info.plist, you should manually set the APNs token in your application
* delegate’s application(_:didRegisterForRemoteNotificationsWithDeviceToken:) method.
*
* If you would like to set the type of the APNs token, rather than relying on automatic
* detection, provide a type of either 'prod', 'sandbox'. Omitting the type parameter
* or specifying 'unknown' will rely on automatic type detection based on provisioning profile.
*
* At a native level you may also call objective-c `[FIRMessaging setAPNSToken];` as needed
*
* > You can safely call this method on Android without platform checks. It's a no-op on Android and will promise resolve `null`.

I'll see what I can about getting the rest of the info to actually show up on the reference site as it should.

@Spxc
Copy link

Spxc commented Feb 16, 2023

According to the docs setAPNS needs to be called,

I don't believe this statement is true? The docs should say (and I think they say?) that you must call setAPNS *if you have disabled method swizzling or for some other reason have disabled the automatic firebase APNS token handling on remote message registration

In almost all cases, certainly the "default integration" cases, if you call the API to register for remote notifications, and have @react-native-firebase/messaging installed in the default manner with correct entitlements on your iOS project to allow them, the SDK will request an APNS token from Apple and set it into the SDK for you fully automated.

You only want or need to use setAPNS token if you really want to manage APNS tokens yourself (for automated testing on non-Apple-Silicon emulators, perhaps, or if you have some brownfield app where you manage APNS tokens yourself, etc)

Hi Mike,
Thanks for the reply!

According to the changelog there were some breaking changes:

[17.0.0](2023-02-02)
⚠ BREAKING CHANGES
app, ios: You must have an APNS token before calling getToken to get an FCM token on iOS. Previously it was not required. See documentation for setAPNSToken if you are using getToken in testing or have disabled FCM Swizzling, and use setAPNSToken to set a token before using getToken

EDIT: Saw your reply, i'll have a read through. The thing is, nothing changed in our code, we just updated the package, then it started throwing the APNS error. Been working flawlessly prior to this :)

How i noticed it; the entire app hung on the splash screen (both simulator and device - iOS). Moving the getToken() function to after the app checks for permissions, fixed the freeze. However it's throwing the error:
Failed to fetch FCM: Error: [messaging/unknown] The operation couldn’t be completed. No APNS token specified before fetching FCM Token

@mikehardy
Copy link
Collaborator

Yes, as you quote, I quote the important bit:

See documentation for setAPNSToken if you are using getToken in testing or have disabled FCM Swizzling

That is

if you are using getToken in testing or have disabled FCM Swizzling

Almost no one does either. In the testing case, you can inject a "correctly formatted but useless" APNS token just to exercise FCM APIs, but who would do that except someone that implements FCM APIs (that is: us here, as maintainers...). So it's possible but I expect no one else to do it.

And disabling swizzling is something I'd consider "advanced" on the iOS side. If you're disabling swizzling you are effectively on your own as you have moved past the default + works implementation and are stating clearly (by disabling swizzling): "we know what we're doing on iOS, don't worry about us"

@mikehardy
Copy link
Collaborator

EDIT: Saw your reply, i'll have a read through. The thing is, nothing changed in our code, we just updated the package, then it started throwing the APNS error. Been working flawlessly prior to this :)

--> #6893 (comment)

@Spxc
Copy link

Spxc commented Feb 16, 2023

Yes, as you quote, I quote the important bit:

See documentation for setAPNSToken if you are using getToken in testing or have disabled FCM Swizzling

That is

if you are using getToken in testing or have disabled FCM Swizzling

Almost no one does either. In the testing case, you can inject a "correctly formatted but useless" APNS token just to exercise FCM APIs, but who would do that except someone that implements FCM APIs (that is: us here, as maintainers...). So it's possible but I expect no one else to do it.

And disabling swizzling is something I'd consider "advanced" on the iOS side. If you're disabling swizzling you are effectively on your own as you have moved past the default + works implementation and are stating clearly (by disabling swizzling): "we know what we're doing on iOS, don't worry about us"

Yeah, but the thing is; this code have been running fine in a simulator upon until today when doing a package update, then the freeze, now the error. This also occurred on a real device

@mikehardy
Copy link
Collaborator

when doing a package update

...across a breaking change boundary, which contains this note:

https://github.com/invertase/react-native-firebase/blob/main/CHANGELOG.md#1700-2023-02-02

app, ios: You must have an APNS token before calling getToken to get an FCM token on iOS. Previously it was not required. See documentation for setAPNSToken if you are using getToken in testing or have disabled FCM Swizzling, and use setAPNSToken to set a token before using getToken

It's a big deal, it must be handled.

Getting an APNS token is an asynchronous network-bound process that could fail, or may even be disabled by some configuration issue.

Previously these misconfigurations or order-of-operations issues were ignored and Firebase would gladly vend an FCM token that was useless as it had no corresponding APNS counterpart.

Now it is (correctly, but maybe painfully) saying "Nope, an FCM token in this context is actually without value, so we won't give you an FCM token without the APNS token behind it"

And we all have to adjust by making sure the APNS token is really there before asking for an FCM token. Might require checking network, re-trying, verifying you really register for remote notifications, you really have your iOS app entitlements correct, you have permission etc, but that's all justified and correct. Just a bit painful, maybe unexpected.

@Spxc
Copy link

Spxc commented Feb 16, 2023

Yeah, but how do we actually check for the token? That's what we are asking about. How can we adjust the code?
We've followed the docs step by step, and it's this simple line:
firebase.messaging().getToken()

Isn't that function supposed to check to see if the APNS are there?

Just want to reiterate: that all the certificates/keys etc are in order

@mikehardy
Copy link
Collaborator

Isn't that function supposed to check to see if the APNS are there?

well...it does, but not in a pleasant way. It checks and throws an error if it's not there. Shouldn't be hanging you should be getting the error I think you report:

The operation couldn’t be completed. No APNS token specified before fetching FCM Token

So, perhaps you want to make sure you have permissions from the user, that you register for remote notifications, and that getAPNSToken returns a token (any token, value doesn't matter - except undefined is bad of course). Once those work (perhaps in a backoff retry loop with a final error to user saying some functionality won't work?) then you go for the FCM token

@Spxc
Copy link

Spxc commented Feb 16, 2023

So, perhaps you want to make sure you have permissions from the user, that you register for remote notifications, and that getAPNSToken returns a token (any token, value doesn't matter - except undefined is bad of course). Once those work (perhaps in a backoff retry loop with a final error to user saying some functionality won't work?) then you go for the FCM token

Awesome, yes, that's exactly what I ended up doing.

Good to know that it handles the error kinda weird. The hanging/freezing issue was fixed by moving the getToken() function after requesting permissions from the user.

@flashman2
Copy link

Got the same error after upgrade
"@react-native-firebase/messaging": "^14.7.0" -> "@react-native-firebase/messaging": "^17.3.0"
Only iOS issue, on Android seems everything is fine.

Signing & Capabilities -> Background Modes (Remote notifications, Background processing) enabled.

Before this upgrade everything was fine. Is there any solution for this issue?

I see this is a new issue but I think a lot of people will see this issue after upgrades. So any workaround, patch or instruction is really needed. Right now, I decided to stick with old version of module.

@Iamivan1996
Copy link

I get this error when update the app from Testflight, i am using Ver 17.3.2.

I found that when i install the previous app and get the fcmToken successfully . After that , i update the app from Testlight, then the getToken() function return this error.

@DanielDanaee
Copy link

DanielDanaee commented Jun 5, 2023

I also tried getting the token in a setTimeout after 3000ms, seems to be reliable now. No clue why this would happen outside of Chrome V8 Engine as the server....

@kg-currenxie
Copy link

kg-currenxie commented Jun 8, 2023

Same, getting it the first time (full app deletion and installation) works. Killing and restarting gets the error :|

📱(real device)

@riku99
Copy link

riku99 commented Jun 8, 2023

I solved the problem by updating mac os and installing additional components of Xcode that were requested after the update.

@thanksyouall
Copy link

I removed my app from my device and built it again and it worked.

@cunguyen-agilityio
Copy link

cunguyen-agilityio commented Jun 20, 2023

Sometimes I got this, anyone here can share a solution?

@kg-currenxie
Copy link

After updating to version 17.4.3, I'm getting the same error, first new install giving token correctly but after killed app and opened app giving above issue.

Please help on this

@sandeep14 any luck for you?

@sandeep14
Copy link

After updating to version 17.4.3, I'm getting the same error, first new install giving token correctly but after killed app and opened app giving above issue.
Please help on this

@sandeep14 any luck for you?

after calling in timeout of 2000 sec then we getting token for every time

@kg-currenxie
Copy link

after calling in timeout of 2000 sec then we getting token for every time

Thx for your reply. Did not work for me tho :/

@sjonchhe
Copy link

Is there really no solution for this yet???

@SubvertDev
Copy link

SubvertDev commented Jul 11, 2023

I'm having similar issue in a Unity project, downgrading Firebase to 10.2.0 helped in my case. Maybe it's working in a later versions, but I didn't have enough time yet to check which ones are working too. Hope it helps someone!

@faustoct1
Copy link

Any solution for this problem? Setup a project from scratch released on store, when it comes to push notification, i set the apns but nothing make it work. It's very trick.

@Harikarthyk
Copy link

I got that error after configuring the react-native-splash-screen, and before getting the token, I waited for 1000 ms, then it worked fine.

@ashankaushalya97
Copy link

I got sorted it out by adding the <key>FirebaseAppDelegateProxyEnabled</key> <true /> property in info.plist file.

@github-actions
Copy link

github-actions bot commented Sep 5, 2023

Hello 👋, to help manage issues we automatically close stale issues.

This issue has been automatically marked as stale because it has not had activity for quite some time.Has this issue been fixed, or does it still require attention?

This issue will be closed in 15 days if no further activity occurs.

Thank you for your contributions.

@github-actions github-actions bot added the Type: Stale Issue has become stale - automatically added by Stale bot label Sep 5, 2023
@glency-betterteem
Copy link

if you are on iOS simulator, i just quit the simulator device and rerun again. I experienced the error when i tried to simulate notification via xcrun simctl push

@github-actions github-actions bot removed the Type: Stale Issue has become stale - automatically added by Stale bot label Sep 6, 2023
@skizzo
Copy link

skizzo commented Sep 6, 2023

Could somebody please point me into a direction that explains what to enter as a parameter in
await messaging().setAPNSToken('XXX');?

From what I understood, this is a required step in the latest version, but I can't find any info about how to obtain that value, calling getToken() always results in the error

[Error: [messaging/unregistered] You must be registered for remote messages before calling getToken, see messaging().registerDeviceForRemoteMessages().]

@Pattygeek
Copy link

I got sorted it out by adding the <key>FirebaseAppDelegateProxyEnabled</key> <true /> property in info.plist file.

this worked for me.

@appa-gomi
Copy link

It seems to me that getToken() doesn't get it right away after starting the app.
You can get it after about 2-3 seconds.
I first checked whether the APNsToken was set using getAPNsToken(), and then executed getToken() when the setting was complete.
And you add the following to the getAPNsToken() Method in node_modules/@react-native-firebase/messaging/ios/RNFBMessaging/RNFBMessagingModule.m.

@@ -188,6 +188,7 @@ RCT_EXPORT_METHOD(getAPNSToken : (RCTPromiseResolveBlock)resolve : (RCTPromiseRe
                            }];
       return;
     }
+    resolve([NSNull null]);
   }
 }

If you do not do this, getAPNsToken() will not return on iOS.

@chenweigh
Copy link

chenweigh commented Sep 25, 2023

same +1

//firebase.json
{
  "react-native": {
    "messaging_ios_auto_register_for_remote_messages": false
  }
}
//react-native code 
await messaging().registerDeviceForRemoteMessages()
const fcmToken = await messaging().getToken()

I nedd to getToken send to server.

when the key messaging_ios_auto_register_for_remote_messages is true, everything is ok.

react-native-firebase: 18.4.0
react-native: 0.72.3

I need this configuration key to turn notification on or off.
Example switch button, turn on registerDeviceForRemoteMessages, turn off unregisterDeviceForRemoteMessages.

but when the key is true, the switch is false, and ture it on, I got warning
Usage of "messaging().registerDeviceForRemoteMessages()" is not required. You only need to register if auto-registration is disabled in your 'firebase.json' configuration file via the 'messaging_ios_auto_register_for_remote_messages' property.

@eggybot
Copy link

eggybot commented Sep 25, 2023

question, base on some search I found that getting APNS token should be working on Simulator but I tried all settings and configuration on the documentation, it didn't work and still get the error/issue on the APNS Token.

Also, on the documentation in this link it says

Firebase Cloud Messaging integrates with the Apple Push Notification service (APNs), however APNs only works with real devices.

Is Cloud Messaging allowed in Simulator or should I actually test on the real device?

As there not clear answer, I switch to real devices and it all works

@skizzo
Copy link

skizzo commented Sep 26, 2023

Good point @eggybot, unfortunately the docs don't 100% clearly state whether it's even possible to get a messaging token on the iOS simulator:

Integrating the Cloud Messaging module on iOS devices requires additional setup before your devices receive messages.

It says "iOS devices", so I guess it's normal to get the "You must be registered for remote messages..." error on the iOS simulator. Would be cool to clarify this though, maybe the man @mikehardy himself could give us a hint about this? ;)

@mikehardy
Copy link
Collaborator

mikehardy commented Sep 28, 2023

The iOS Simulator can get an APNs token and do messaging, if it is iOS 16+ and it is running on an Apple Silicon machine and macOS is 13+:

https://github.com/invertase/react-native-firebase/blob/main/CHANGELOG.md#features-11

Which leads to this chunk of diff:

8d75b36#diff-348d828b3a27617f7b287d2f055dfe4641effd95bdf21cbfb87397c4675dea7fR22-R27

Where you can see the conditions that must be met and how I exercise this new (and pretty cool) feature as I implemented it for react-native-firebase v17 since I was eager to actually see this work and prove that it (and our implementation here) worked. It does!

In all other cases you need to make up a token and send it through setAPNSToken in order to try do anything with the FCM system, and you will not actually receive messages.

Sadly, I missed that chunk of documentation someone just linked, and I've posted PR #7381 to clarify it - I'll merge it shortly if no one has any comments but I'll leave it open for review in case anyone has a better of way of clarifying it, just post up a comment on the PR

Hope this helps

@Rodney-Web
Copy link

Rodney-Web commented Sep 28, 2023

100% Fixed my issue by:

  1. setting FirebaseAppDelegateProxyEnabled to NO in Info.plist

  2. Go to AppDelegate.mm and add the following inside didFinishLaunchingWithOptions function:
    [application registerForRemoteNotifications];

  3. Go to didRegisterForRemoteNotificationsWithDeviceToken function and add this:
    [FIRMessaging messaging].APNSToken = deviceToken;

  4. Clean build and start build again.

(FYI, make sure to use real device for testing.)

Hope it works for you guys.

FYI:
If you are using Firebase dynamic links for your project you can just switch the FirebaseAppDelegateProxyEnabled to YES as it causes issues with dynamic links.

@dexiouz
Copy link

dexiouz commented Oct 4, 2023

3. didRegisterForRemoteNotificationsWithDeviceToken

Hi please where do I add or find this "didRegisterForRemoteNotificationsWithDeviceToken"

@rhfksl
Copy link

rhfksl commented Oct 5, 2023

  1. didRegisterForRemoteNotificationsWithDeviceToken

Hi please where do I add or find this "didRegisterForRemoteNotificationsWithDeviceToken"

I setup "didRegisterForRemoteNotificationsWithDeviceToken" when I install @react-native-community/push-notification-ios

// Required for the register event.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
  [FIRMessaging messaging].APNSToken = deviceToken;
 [RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

@samih-dev
Copy link

samih-dev commented Oct 8, 2023

Rodney-Web + rhfksl method worked for me,
for those whom don't use @react-native-community/push-notification-ios, just comment OUT the code related:
[RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help: Needs Triage Issue needs additional investigation/triaging. Impact: Bug New bug report
Projects
None yet
Development

No branches or pull requests