Data-only messages are not captured by messaging().onMessage on IOS
Issue
Describe your issue here After upgrading all react-native-firebase dependancies to the latest versions (6.4.3), the messaging module's onMessage handler does not fire for data-only messages. This is the case for both foreground and background handling. However, the behaviour is normal when I include the notification payload into my message. As long as a notification payload is provided, the listener fires. I've purchased Notifee and I'm trying to integrate a data only approach to client display of notifications, but this is a huge roadblock if I'm unable to send the data through. I haven't tested the behaviour on android yet.
I suspect that this may have something to do with the remote notifications swizzling. I'm receiving this prompt from the xcode console
FIRMessaging Remote Notifications proxy enabled, will swizzle remote notification receiver handlers. If you'd prefer to manually integrate Firebase Messaging, add "FirebaseAppDelegateProxyEnabled" to your Info.plist, and set it to NO. Follow the instructions at:
https://firebase.google.com/docs/cloud-messaging/ios/client#method_swizzling_in_firebase_messaging
to ensure proper integration.
- This is the first issue that I'm posting. Please forgive me if it isn't up to standards. I'll happily add more info if required.
Project Files
Javascript
index.js
// Register background handler
messaging().setBackgroundMessageHandler(async (m) => {
await notifications.handleNotification(m, true);
});
app.js
React.useEffect(() => {
const unsubscribe = messaging().onMessage(async (m) => {
console.log('received');
await notifications.handleNotification(m);
return () => unsubscribe();
});
}, []);
package.json:
"dependencies": {
"@notifee/react-native": "^0.10.0",
"@react-native-community/async-storage": "^1.11.0",
"@react-native-community/datetimepicker": "^2.6.0",
"@react-native-community/masked-view": "^0.1.10",
"@react-native-community/picker": "^1.6.5",
"@react-native-firebase/analytics": "^7.3.1",
"@react-native-firebase/app": "^8.2.0",
"@react-native-firebase/dynamic-links": "^7.3.2",
"@react-native-firebase/messaging": "^7.5.0",
"@react-navigation/bottom-tabs": "^5.7.2",
"@react-navigation/native": "^5.7.1",
"@react-navigation/stack": "^5.7.1",
"jetifier": "^1.6.6",
"react": "16.13.1",
"react-native": "^0.63.2",
"react-native-dotenv": "^0.2.0",
"react-native-gesture-handler": "^1.7.0",
"react-native-modal": "^11.5.6",
"react-native-reanimated": "^1.10.1",
"react-native-safe-area-context": "^3.1.1",
"react-native-screens": "^2.9.0",
"react-native-svg": "^12.1.0",
"react-native-toaster": "git+https://github.com/HeroAI-Team/react-native-toaster.git",
"react-native-vector-icons": "^7.0.0",
"react-redux": "^7.2.1",
"redux": "^4.0.5",
"redux-thunk": "^2.3.0",
"reselect": "^4.0.0"
},
"devDependencies": {
"@babel/core": "^7.10.5",
"@babel/runtime": "^7.10.5",
"@react-native-community/eslint-config": "2.0.0",
"babel-jest": "^26.1.0",
"eslint": "^7.5.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-prettier": "^3.1.4",
"jest": "^26.1.0",
"metro-react-native-babel-preset": "^0.61.0",
"prettier": "^2.0.5",
"react-scripts": "3.4.1",
"react-test-renderer": "16.13.1",
"redux-devtools": "^3.5.0"
},
# N/A
firebase.json for react-native-firebase v6:
"@react-native-firebase/analytics": "^7.3.1",
"@react-native-firebase/app": "^8.2.0",
"@react-native-firebase/dynamic-links": "^7.3.2",
"@react-native-firebase/messaging": "^7.5.0",
# N/A
iOS
All required capabilities have been set up in xcode.
ios/Podfile:
- [ ] I'm not using Pods
- [x] I'm using Pods and my Podfile looks like:
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'
require_relative '../node_modules/react-native/scripts/react_native_pods'
platform :ios, '10.0'
target 'HeroAIBeacon' do
config = use_native_modules!
use_react_native!(:path => config["reactNativePath"])
# Enables Flipper.
#
# Note that if you have use_frameworks! enabled, Flipper will not work and
# you should disable these next few lines.
use_flipper!
post_install do |installer|
flipper_post_install(installer)
end
end
# N/A
AppDelegate.m:
#import "AppDelegate.h"
#import <Firebase.h>
#import <React/RCTBridge.h>
#import <React/RCTLinkingManager.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
moduleName:@"HeroAIBeacon"
initialProperties:nil];
rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [UIViewController new];
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
// Firebase app
[FIRApp configure];
return YES;
}
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
#else
return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
return [RCTLinkingManager application:application openURL:url options:options];
}
- (BOOL)application:(UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity
restorationHandler:(nonnull void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler
{
return [RCTLinkingManager application:application
continueUserActivity:userActivity
restorationHandler:restorationHandler];
}
@end
// N/A
Android
Click To Expand
Have you converted to AndroidX?
- [ ] my application is an AndroidX application?
- [ ] I am using
android/gradle.settingsjetifier=truefor Android compatibility? - [ ] I am using the NPM package
jetifierfor 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
react-native info output:
OUTPUT GOES HERE
- Platform that you're experiencing the issue on:
- [x] iOS
- [ ] Android
- [x] iOS but have not tested behavior on Android
- [ ] Android but have not tested behavior on iOS
- [ ] Both
react-native-firebaseversion you're using that has this issue:6.4.0
Firebasemodule(s) you're using that has the issue:Cloud Messaging
- Are you using
TypeScript?N
- 👉 Check out
React Native FirebaseandInvertaseon Twitter for updates on the library.
Have you checked similar issues? There are so many about iOS background handling. This one might be the most thorough https://github.com/invertase/react-native-firebase/issues/3367#issuecomment-605907816
You'll want to have the device plugged in and watch the log so as not to miss anything
@mikehardy Thanks for the feedback. I've figured out the problem. When sending the notification payload through the python Firebase_admin service, I added an APNS config enabling the content_available field to ensure the app runs on background mode. For some reason, this additional payload was preventing the data only message from being received on the client side. After I removed it, I'm now able to receive both foreground and background messages through the onMessage() handler.
Commenting out the APNS and Android configurations
android = messaging.AndroidConfig(priority='high')
apns = messaging.APNSConfig(
headers={
':method': 'POST',
'apns-priority': '10',
'apns-push-type': 'alert',
},
payload=messaging.APNSPayload(aps=messaging.Aps(content_available=True))
)
message = messaging.Message(
data=data,
token=token,
# apns=apns,
# android=android,
)
# Send a message to the device corresponding to the provided registration token.
response = messaging.send(message)
I think this is a potentially dangerous solution, actually. Why would removing the header information (especially since the absence of that header information has caused a lot of problems for people in the past) solve this problem? Does the python admin SDK do something that the Node admin SDK doesn't? May be worth looking into. I'm also wondering why you also commented out the Android configs if the issue was for iOS.
It also may be that your header configuration was actually incorrect for data-only messages that appear in both backgorund and foreground. After reading the APNS docs and the RNFB docs, I settled on this (not tested yet, but should work):
apns: {
payload: {
aps: {
contentAvailable: true //Gives message priority to be seen in background and quit state
}
},
headers: {
'apns-push-type': 'background', //Since notifications will be handled locally by the client, no need to make this 'alert'
'apns-priority': '5', //Has to be 5 because of contentAvailable, though 10 would be ideal
'apns-topic': //your app bundle identfier
}
}
@andersonaddo Thanks for the reply. After some more testing, the issue wasn't actually in the APNS config, but rather the headers that I was providing. Like you said, I was a bit worried about removing the header information, but after testing with your configurations, it seems to work perfectly. I'll make sure to read through the docs more thoroughly next time.
Great! Glad I could help.