[π] iOS - Cannot receive any notifications before the app opens second time
Background
Here I am changing the notification service from appcenter to firebase.
I am building the app to my real device (iPhone XR) using Xcode in order to test notifications.
I have tried to search the issue from this issues board, stack overflow, and google, but still cannot find any related reports.
Project Files
index.js
import messaging from '@react-native-firebase/messaging';
messaging().setBackgroundMessageHandler(async (remoteMessage) => {});
import App from './App';
AppRegistry.registerComponent('sampleApp', () => App)
App.js
import messaging from '@react-native-firebase/messaging';
class App extends Component {
requestUserPermission = async () => {
const authStatus = await messaging().requestPermission();
const enabled =
authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
authStatus === messaging.AuthorizationStatus.PROVISIONAL;
console.log("notification authorized?", enabled);
};
async componentDidMount() {
await this.requestUserPermission();
/* Get the token and store in database */
messaging().getToken().then((token) => {
console.log("fcm token, need to store to backend database", token);
});
/* Success */
const unsubscribe = messaging().onMessage(async remoteMessage => {
console.log("Notification when app is on foreground", remoteMessage);
});
/* Success */
messaging().onNotificationOpenedApp(remoteMessage => {
console.log('Notification caused app to open from background state:', remoteMessage);
});
/* Success */
messaging().getInitialNotification().then(remoteMessage => {
if (remoteMessage) {
console.log('Notification caused app to open from quit state:', remoteMessage);
}
});
}
render() {
return (/* Blah blah blah */);
}
}
export default App;
I have also built up a firebase-admin nodejs server for pushing notification:
const admin = require('firebase-admin');
const credential = require('./sample-firebase-adminsdk-aaaaaaaaaaa.json');
admin.initializeApp({ credential: admin.credential.cert(credential) });
/*
*/
function sendNotification(targets, title, message, badge = 1, imageUrl = null) {
if (!Array.isArray(targets) || targets.length == 0) {
throw new Error("Please provide non-empty targets tokens as Array.");
}
if (!title || !message) {
throw new Error("Please provide title and message");
}
let notification = {
tokens: targets,
notification: {
title: title,
body: message
},
data: {},
apns: {
payload: {
aps: {
'mutable-content': 1,
badge: badge,
sound: 'default',
}
}
}
};
if (imageUrl) {
notification.apns.fcm_options = { image: imageUrl };
notification.notification.imageUrl = imageUrl;
notification.android = { notification: { image: imageUrl } };
}
admin.messaging().sendMulticast(notification).then((response) => {
console.log('Successfully sent message:', response);
}).catch((error) => {
console.log('Error sending message:', error);
});
}
Then I will call the function like below:
sendNotification(['device_firebase_message_token'], 'Hello World', 'If you see me, it is success!', 1, 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png');
I surely follow the instructions to install the react-native-firebase and all firebase-cloud-messaging steps in https://rnfirebase.io/. (Also the image notification in iOS)
Issue Encountered
After I have built the app into my real device(iPhone XR) through Xcode,
I will open the debugger to get the fcm token from console.log(). No matter:
- I push the notification through
firebase consoleor my own builtnodejs function - The app is foreground, background or quit state
it cannot receive any notifications. The logs in nodejs function show it is success:
Successfully sent message: {
responses: [
{
success: true,
messageId: 'projects/sample-app/messages/0:xxxxxxxxxxxxxxxx'
}
],
successCount: 1,
failureCount: 0
}
But after I completely quit the app from background and restart the app again, it can receive notifications normally. The logs also show it is success:
Successfully sent message: {
responses: [
{
success: true,
messageId: 'projects/sample-app/messages/xxxxxxxxxxxxxxxx'
}
],
successCount: 1,
failureCount: 0
}
One thing I have noticed is: the message ID are always start with 0: when I cannot receive the notification.
Does anyone encountered this problem? And how to solve it?
Environment
Click To Expand
react-native info output:
React Native Environment Info:
System:
OS: macOS 10.15.6
CPU: (8) x64 Intel(R) Core(TM) i7-8569U CPU @ 2.80GHz
Memory: 181.42 MB / 16.00 GB
Shell: 5.7.1 - /bin/zsh
Binaries:
Node: 13.14.0 - /usr/local/bin/node
Yarn: 1.19.1 - /usr/local/bin/yarn
npm: 6.14.4 - /usr/local/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 14.0, DriverKit 19.0, macOS 10.15, tvOS 14.0, watchOS 7.0
Android SDK:
API Levels: 23, 24, 25, 26, 27, 28, 29
Build Tools: 26.0.2, 26.0.3, 27.0.3, 28.0.3, 29.0.2, 29.0.3
System Images: android-28 | Intel x86 Atom_64, android-28 | Google APIs Intel x86 Atom, android-28 | Google Play Intel x86 Atom
IDEs:
Android Studio: 3.5 AI-191.8026.42.35.5791312
Xcode: 12.0/12A7209 - /usr/bin/xcodebuild
npmPackages:
react: 16.8.3 => 16.8.3
react-native: 0.59.10 => 0.59.10
npmGlobalPackages:
create-react-native-module: 0.19.0
react-native-cli: 2.0.1
react-native-rename: 2.4.1
- Platform that you're experiencing the issue on:
- [x] iOS but have not tested behavior on Android
- Dependencies in
package.json
"@react-native-firebase/app": "8.4.1",
"@react-native-firebase/messaging": "7.8.4",
"react": "16.8.3",
"react-native": "0.59.10",
"react-redux": "5.0.7",
"redux": "3.7.2",
Try doing it without remote debugger, that causes the JS bundle to run in browser vs on device and the execution environment is pretty different. Also, you may need to wait a little bit - these processes are all asynchronous in the cloud though your success response from node does seem to indicate it should work.
I was having the same issue here on iOS with latest React Native and it seemed to persist even after uploading a production build to TestFlight. Downgrading the libraries solved the issue for me:
"@react-native-firebase/app": "^8.4.1" => "@react-native-firebase/app": "~8.2.0",
"@react-native-firebase/messaging": "^7.8.3" => "@react-native-firebase/messaging": "~7.5.0"
On iOS notifications were only received on second app launch and after, Android had no issues at all. I'm using the libraries exactly as @jagwingchoy, though having quite a bit more dependencies. Happy to add my environment details here once I'm back at work if it helps.
Hey,
Just to add that I appear to be having the same issue.
"@react-native-firebase/app": "^8.4.3" "@react-native-firebase/messaging": "^7.8.6"
I've tried using the default Firebase iOS SDK version and also overriding to 6.32.2
On first install and open from TestFlight:
- I can send a notification with the APNS token direct and it's successful.
- If I try and use the FCM token to send a notification via the Firebase console it never appears on the device.
Hard closing the app and re-opening it appears to resolve the issue - sending the same message with the same FCM token via the Firebase console is successful.
Happy to add extra info if helpful π
This sounds like the sort of thing that may need a reproduction using pure Obj-C (or Swift) and nothing but the native APIs to see if it's firebase-ios-sdk or us https://github.com/firebase/quickstart-ios/tree/master/messaging - that is a skeleton which should be trivial to build a repro on
Just to add more info, I am having the same issue with some devices. Works on iPhone 7, ios 14, iPhone 6s but not on X and 11 using the following:
"@react-native-firebase/app": "^8.4.3",
"@react-native-firebase/messaging": "^7.8.6",
Disclaimer: I'm no native developer π
I had a go with the iOS quick start above and it seems to have the same issue which I guess points towards it being a firebase-ios-sdk problem?
This was my process:
- Built and deployed the iOS quick start app to TestFlight.
- Deleted any existing version of the iOS quick start app.
- Restarted device
- Installed the app from TestFlight
- Allowed notifications and retrieved the FCM Token
- Backgrounded the app
- Sent a test message (with just title and notification text set to "Test") to the FCM Token via Firebase console
- No notification received.
- Force quit the app.
- Re-open the app.
- Background the app.
- Send another message via Firebase console to same FCM token
- Notification received
I can repeat the above process and it always seems to be the same.
When testing the app without TestFlight and just running from Xcode to my device I experienced inconsistent results. Sometimes the notification is received on first install/open and other times I have to quit the app and re-open it. However, via TestFlight it seems to be consistently not working following the steps above.
To set expectations: for something inconsistently reproduced or device specific this really doesn't sound like a problem with the module and also sounds quite time-intensive to troubleshoot, I will spend no personal effort on finding the root cause but will happily merge a well-reasoned PR. I strongly suggest using the firebase-ios-sdk quickstart messaging sample to attempt reproduction without this module
for ppl that use performance and analytics, this is the combo that actually works for me(so far):
"@react-native-firebase/analytics": "7.4.2",
"@react-native-firebase/app": "~8.2.0",
"@react-native-firebase/messaging": "~7.5.0",
"@react-native-firebase/perf": "7.3.2",
I have the exact same behaviour. on TestFlight version it never works after first install, you need to force quit the app and open it again to start receiving notificaitons. But on XCode run version it sometimes works after first install other times it never works. I tried every possible hack to fix it over the last week and I am not able to find any solution. Downgrading to App 8.2.0 and Messaging 7.5.0 fixed it but it's not a possible solution for me since it will break other things on my code.
@ahmadworks did you look at the changelog for the versions released between the version you downgraded to and the version not working for you? Did you try versions one by one (if there are more than one) to see exactly when it broke? Then you can go into the actual version (I keep releases very small so it is usually just one PR with a small change) to see what the difference was? Then you can try the current version but overriding the difference (either via patch-package or - more likely it was a firebase-ios-sdk change) to see if it works again
Finally, if it was code here you can point out the actual error, or if it was firebase-ios-sdk upgrading you can check their github for relevant issues and get an actual fix for the problem.
I don't think we changed much here in the versions you mention so this sounds like a firebase-ios-sdk problem. If you need to make a reproduction from them you need to do it based off one of their quickstarts: https://github.com/firebase/quickstart-ios/ - they won't accept react-native issues
@tomkelsey i was having the exact same problem as you -- after a clean install, nothing was being called. force close the app, re-open, and my notifications would start working again
i just downgraded my app to the following versions:
"@react-native-firebase/analytics": "7.4.1",
"@react-native-firebase/app": "~8.2.0",
"@react-native-firebase/messaging": "~7.5.0",
Which in turn, changed some Podfile.lock versioning. This is what it looks like for firebase:
- Firebase/Analytics (6.27.1):
- Firebase/Core (6.27.1):
- Firebase/CoreOnly (6.27.1):
- Firebase/Messaging (6.27.1):
- FirebaseAnalytics (6.6.2):
- FirebaseCore (6.8.1):
- FirebaseCoreDiagnostics (1.5.0):
- FirebaseInstallations (1.5.0):
- FirebaseInstanceID (4.5.1):
- FirebaseMessaging (4.5.0):
I'm not really sure yet if it was related to RNFB or Firebase core. I'm working on their quickstart app to see if i can find anything there.
(btw, after i changed packages.json, i fully removed my node_modules, then i removed my Podfile.lock and then i reinstalled my pods. that did the trick)
I was having the same issue here on iOS with latest React Native and it seemed to persist even after uploading a production build to TestFlight. Downgrading the libraries solved the issue for me:
"@react-native-firebase/app": "^8.4.1" => "@react-native-firebase/app": "~8.2.0", "@react-native-firebase/messaging": "^7.8.3" => "@react-native-firebase/messaging": "~7.5.0"On iOS notifications were only received on second app launch and after, Android had no issues at all. I'm using the libraries exactly as @jagwingchoy, though having quite a bit more dependencies. Happy to add my environment details here once I'm back at work if it helps.
Thanks @adbario, I have downgraded to the versions you mentioned fixed the issue.
@mikehardy please check this (https://github.com/leyserkids/cordova-plugin-firebase/commit/124dd7247dd55159b661759f27491819833bbb5b) it fixed the bug for Cordova users.
@mikehardy please check this (leyserkids/cordova-plugin-firebase@124dd72) it fixed the bug for Cordova users.
I am the author of this commit. this issue has gone after this change, But I am not sure if itβs really works well.
Thanks for the suggestion @ahmadworks and for checking in @ewfian interesting your uncertainty, I remember reading the changelog for the last ios sdk update and thinking "hmm looks like we have a fair bit of work to switch away from any of the instance ID APIs as they're moving that all under messaging", so to me this really looks like it aligns with the future direction of firebase-ios-sdk and if I were the firebase-ios-sdk team I wouldn't even accept the instance ID bug as a repro, I'd just direct you/us to the API in that commit
So to the point: is there anything specific you are uncomfortable with, or would a PR in this repo to make the analogous switch seem like the right thing? Seems like it would be right thing to me...
@tomkelsey i was having the exact same problem as you -- after a clean install, nothing was being called. force close the app, re-open, and my notifications would start working again
i just downgraded my app to the following versions:
"@react-native-firebase/analytics": "7.4.1", "@react-native-firebase/app": "~8.2.0", "@react-native-firebase/messaging": "~7.5.0",Which in turn, changed some Podfile.lock versioning. This is what it looks like for firebase:
- Firebase/Analytics (6.27.1): - Firebase/Core (6.27.1): - Firebase/CoreOnly (6.27.1): - Firebase/Messaging (6.27.1): - FirebaseAnalytics (6.6.2): - FirebaseCore (6.8.1): - FirebaseCoreDiagnostics (1.5.0): - FirebaseInstallations (1.5.0): - FirebaseInstanceID (4.5.1): - FirebaseMessaging (4.5.0):I'm not really sure yet if it was related to RNFB or Firebase core. I'm working on their quickstart app to see if i can find anything there.
(btw, after i changed
packages.json, i fully removed mynode_modules, then i removed myPodfile.lockand then i reinstalled my pods. that did the trick)
Had the same issue and this worked for me, thanks @adbario
Glad to hear there is a workaround but the most valuable thing anyone could do :pray: :pray: is take the change listed above and make a PR here that implemented the same switch to the different/new API
https://github.com/leyserkids/cordova-plugin-firebase/commit/124dd72
We're also facing the same issue here and it seems somewhat dependent on the iDevice, since some of our test devices don't face the issue while others do.
Trying to switch to tokenWithCompletion and deleteDataWithCompletion did not help here, same issue. I'm not sure the mentioned PR really fixes the problem.
Interesting. I was operating under the assumption the referenced change from cordova ecosystem would resolve the issue. @TPXP - are you quite certain you attempted that patch cleanly (for example using npx react-native-clean-project to make absolutely sure the change was in effect? this is a common problem...).
If that PR doesn't fix the problem then, no, I don't just want a PR that rolls back changes, we learn nothing then.
What I want is a bisect on the versions (perhaps already done? I think we know the version to roll back to, indicating that the next version broke?) to know exactly which PRs are the problem (they're not listed yet unless I missed them?)
Then maybe a roll back PR, but I have a further hunch that it is a firebase-ios-sdk issue, so if the change in the PR that caused the problem is just bumping the firebase-ios-sdk we don't need a PR here at all. You just override the firebase-ios-sdk in your Podfile, but then we need a clean reproduction based on the quickstart directly against firebase-ios-sdk so we may get resolution
The general idea being: we can't just move backwards here. The underlying firebase SDKs move too quickly, and when we support the new APIs (which we must do) we become dependent on the new versions, moving back blindly is not a viable plan.
So I'm looking for any way to either stay current or log upstream issues vs current while moving allowing affected people to temporarily stay back.
@mikehardy Here is the patch I applied. It breaks the scope feature but I don't need it in my app. https://gist.github.com/TPXP/c149253c2e549684a1e5abcf466b974d
I'm also betting this is a firebase ios SDK issue, especially since downgrading fixed the issue for some.