Skip to content

[🐛] Bug/notification tap action issue and background updating badge issue in iOS my android is working fine #7958

Closed as not planned
@Jeel-Bhad

Description

@Jeel-Bhad

Issue

I am using '@react-native-firebase/messaging' this package is completely works well in my android(react-native-app)
but I am facing issue in my ios build - my build is generated successfully but after getting notification I am not able to perform any background task.
my 1st task is to redirect my notification to specific page on notification tap
my 2nd task is to update badge in background while coming new notification

my 2 function is not getting fired in ios while receiving notification let me show my code below
my firebase payload for ios
{
"data": {
"buildingId": "27",
"buildingName": "Jeel",
"childSubfolderId": "0",
"diciplineId": "0",
"folderid": "2",
"groupId": "35",
"isfrom": "SubFolder",
"isgroup": "false",
"ispage": "Chat",
"messageId": "4066",
"receiverId": "267",
"receivername": "Jeel",
"senderId": "271",
"sendername": "Sherin M",
"subfolderid": "55"
},
"from": "810581931372",
"messageId": "1722841496653586",
"notification": {
"body": "fgfg",
"ios": {
"badge": 18
},
"sound": "default",
"title": "Sherin M"
}
}

1)task related to badge update-I want to make an API call
index.js file
import 'react-native-gesture-handler';
import {Alert, AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
import messaging from '@react-native-firebase/messaging';
import notifee from '@notifee/react-native';

//Register background handler
messaging().setBackgroundMessageHandler(async remoteMessage => {
console.log('Message handled in the background!', remoteMessage);
dispatch(unReadNotificationCntApi()).unwrap().then((res)=>{
notifee.setBadgeCount(res.data);
}).catch((err)=>{
console.log(err);
Alert.alert(err);
});
});

2)I am not able to redirect my notification after tapping on it and I am calling this below function in my app.tsx file

export async function notificationListner() {
const unsubscribe = messaging().onMessage(async remoteMessage => {

    console.log('Forground : A new FCM message arrived!', remoteMessage);
  });

  messaging().onNotificationOpenedApp( remoteMessage => {
    console.log('tap : Notification from background state:',remoteMessage);
    // You can handle the notification here, for example, navigate to a specific screen based on the notification data
    const fireData=remoteMessage.data
    if(!!fireData?.ispage && typeof fireData?.ispage === 'string' && fireData.ispage.toLocaleLowerCase() == "chat" )
      {
        navigate('SignalR',{
            isGroup: fireData?.isgroup=='true' ? (fireData.isgroup):false,
            receiverId: typeof fireData?.senderId === 'string' ? parseInt(fireData.senderId) : 0,
            userName: fireData?.isgroup ?fireData.buildingName:fireData?.sendername,
            groupId:fireData?.isgroup=='true'? typeof fireData?.groupId === 'string' ?  parseInt(fireData.groupId): 0 : 0,
            isFrom:fireData?.isfrom,
            projectId: fireData?.buildingId ? fireData.buildingId: 0,
            folderId: fireData?.folderid ? fireData.folderid: 0,
            subfolderId: fireData?.subfolderid ? fireData.subfolderid: 0,
            ChildSubFolderId: fireData?.childSubfolderId ? fireData.childSubfolderId: 0
          });
        }else if(!!fireData?.ispage && typeof fireData?.ispage === 'string' && fireData.ispage.toLowerCase() == "approval" ){
          navigate('Document',{
            projectId: fireData.buildingId, 
            folderId: fireData?.folderid, 
            subfolderId: fireData?.subfolderid, 
            ChildSubFolderId: fireData?.childSubfolderId
          })
        }
        else if(typeof fireData?.ispage === 'string' && fireData.ispage.toLowerCase() != "approval" && fireData.ispage.toLocaleLowerCase() != "chat"){
          navigate('Dashboard', {projectId: fireData?.buildingId});
        }
  });

  messaging().getInitialNotification().then( async remoteMessage => {
    await notifee.setBadgeCount(555)
    console.log('quit state : getInitialNotification',remoteMessage);
  });
  return unsubscribe;

}

Describe your issue here

if I want to perform any API call inside the following function I am not able to do that.
even I am not getting this console.log value after coming my notification.
console.log('Message handled in the background!', remoteMessage);
messaging().setBackgroundMessageHandler

for second issue I am not able to fire code inside messaging().onNotificationOpenedApp() function

Project Files

Javascript

import messaging from '@react-native-firebase/messaging';
import { setLocalStorageValue } from './helpers';
import { navigate } from '../navigations/RootNavigation';
import { Alert } from 'react-native';
import notifee from '@notifee/react-native' ;
export async function requestUserPermission() {
const authStatus = await messaging().requestPermission();
const enabled =
authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
authStatus === messaging.AuthorizationStatus.PROVISIONAL;

if (enabled) {
  console.log('Authorization status:', authStatus);
  Alert.alert("authStatus"+authStatus)
  getToken();
}

}

const getToken = async() =>{
try{
const token = await messaging().getToken();
token && await setLocalStorageValue({field: 'firebaseToken', data: token});
Alert.alert("token"+token)
console.log(token);
}catch{
console.log("Error getting token");
}
}

export async function notificationListner() {
const unsubscribe = messaging().onMessage(async remoteMessage => {

    console.log('Forground : A new FCM message arrived!', remoteMessage);
  });

  messaging().onNotificationOpenedApp( remoteMessage => {
    console.log('tap : Notification from background state:',remoteMessage);
    // You can handle the notification here, for example, navigate to a specific screen based on the notification data
    const fireData=remoteMessage.data
    if(!!fireData?.ispage && typeof fireData?.ispage === 'string' && fireData.ispage.toLocaleLowerCase() == "chat" )
      {
        navigate('SignalR',{
            isGroup: fireData?.isgroup=='true' ? (fireData.isgroup):false,
            receiverId: typeof fireData?.senderId === 'string' ? parseInt(fireData.senderId) : 0,
            userName: fireData?.isgroup ?fireData.buildingName:fireData?.sendername,
            groupId:fireData?.isgroup=='true'? typeof fireData?.groupId === 'string' ?  parseInt(fireData.groupId): 0 : 0,
            isFrom:fireData?.isfrom,
            projectId: fireData?.buildingId ? fireData.buildingId: 0,
            folderId: fireData?.folderid ? fireData.folderid: 0,
            subfolderId: fireData?.subfolderid ? fireData.subfolderid: 0,
            ChildSubFolderId: fireData?.childSubfolderId ? fireData.childSubfolderId: 0
          });
        }else if(!!fireData?.ispage && typeof fireData?.ispage === 'string' && fireData.ispage.toLowerCase() == "approval" ){
          navigate('Document',{
            projectId: fireData.buildingId, 
            folderId: fireData?.folderid, 
            subfolderId: fireData?.subfolderid, 
            ChildSubFolderId: fireData?.childSubfolderId
          })
        }
        else if(typeof fireData?.ispage === 'string' && fireData.ispage.toLowerCase() != "approval" && fireData.ispage.toLocaleLowerCase() != "chat"){
          navigate('Dashboard', {projectId: fireData?.buildingId});
        }
  });

  messaging().getInitialNotification().then( async remoteMessage => {
    await notifee.setBadgeCount(555)
    console.log('quit state : getInitialNotification',remoteMessage);
  });
  return unsubscribe;

}

package.json:

# N/A

{
"name": "taskxs",
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"lint": "eslint .",
"start": "react-native start",
"test": "jest"
},
"dependencies": {
"@expo/react-native-action-sheet": "^4.0.1",
"@fortawesome/fontawesome-svg-core": "^6.5.1",
"@fortawesome/free-solid-svg-icons": "^6.5.1",
"@fortawesome/react-native-fontawesome": "^0.3.0",
"@microsoft/signalr": "^8.0.0",
"@notifee/react-native": "^7.8.2",
"@react-native-async-storage/async-storage": "^1.22.3",
"@react-native-community/push-notification-ios": "^1.11.0",
"@react-native-firebase/app": "^19.2.2",
"@react-native-firebase/messaging": "^19.2.2",
"@react-navigation/drawer": "^6.6.14",
"@react-navigation/native": "^6.1.12",
"@react-navigation/stack": "^6.3.28",
"@reduxjs/toolkit": "^2.2.1",
"axios": "^1.6.7",
"i18next": "^23.10.1",
"moment": "^2.30.1",
"react": "18.2.0",
"react-hook-form": "^7.51.0",
"react-i18next": "^14.1.0",
"react-native": "0.73.4",
"react-native-element-dropdown": "^2.10.2",
"react-native-gesture-handler": "^2.15.0",
"react-native-image-picker": "^7.1.2",
"react-native-localize": "^3.0.6",
"react-native-pager-view": "^6.3.3",
"react-native-paper": "^5.12.3",
"react-native-reanimated": "^3.8.0",
"react-native-render-html": "^6.3.4",
"react-native-safe-area-context": "^4.9.0",
"react-native-screens": "^3.29.0",
"react-native-splash-screen": "^3.3.0",
"react-native-svg": "^15.1.0",
"react-native-toast-message": "^2.2.0",
"react-redux": "^9.1.0",
"rxjs": "^7.8.1"
},
"devDependencies": {
"@babel/core": "^7.20.0",
"@babel/preset-env": "^7.20.0",
"@babel/runtime": "^7.20.0",
"@react-native/babel-preset": "0.73.21",
"@react-native/eslint-config": "0.73.2",
"@react-native/metro-config": "0.73.5",
"@react-native/typescript-config": "0.73.1",
"@types/react": "^18.2.6",
"@types/react-test-renderer": "^18.0.0",
"babel-jest": "^29.6.3",
"eslint": "^8.19.0",
"jest": "^29.6.3",
"prettier": "2.8.8",
"react-test-renderer": "18.2.0",
"typescript": "5.0.4"
},
"engines": {
"node": ">=18"
}
}

firebase.json for react-native-firebase v6:

# N/A
```index.js file
import 'react-native-gesture-handler';
import {Alert, AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
import messaging from '@react-native-firebase/messaging';
import notifee from '@notifee/react-native';
//Register background handler
messaging().setBackgroundMessageHandler(async remoteMessage => {
    console.log('Message handled in the background!', remoteMessage);
    dispatch(unReadNotificationCntApi()).unwrap().then((res)=>{
      notifee.setBadgeCount(res.data)
    }).catch((err)=>{
      console.log(err);
      Alert.alert(err);
    });
    
  });
 
AppRegistry.registerComponent(appName, () => App);


</details>

### iOS

<details><summary>Click To Expand</summary>
<p>

#### `ios/Podfile`:

- [ ] I'm not using Pods
- [x] I'm using Pods and my Podfile looks like: [ ] 
# Resolve react_native_pods.rb with node to allow for hoisting
require Pod::Executable.execute_command('node', ['-p',
  'require.resolve(
    "react-native/scripts/react_native_pods.rb",
    {paths: [process.argv[1]]},
  )', __dir__]).strip

platform :ios, min_ios_version_supported
prepare_react_native_project!

# If you are using a `react-native-flipper` your iOS build will fail when `NO_FLIPPER=1` is set.
# because `react-native-flipper` depends on (FlipperKit,...) that will be excluded
#
# To fix this you can also exclude `react-native-flipper` using a `react-native.config.js`
# js
# module.exports = {
#   dependencies: {
#     ...(process.env.NO_FLIPPER ? { 'react-native-flipper': { platforms: { ios: null } } } : {}),
# 
#flipper_config = ENV['NO_FLIPPER'] == "1" ? FlipperConfiguration.disabled : FlipperConfiguration.enabled

linkage = ENV['USE_FRAMEWORKS']
if linkage != nil
  Pod::UI.puts "Configuring Pod with #{linkage}ally linked Frameworks".green
  use_frameworks! :linkage => linkage.to_sym
end

use_modular_headers!
target 'TaskXS' do
  config = use_native_modules!
  
  pod 'Firebase/Core', :modular_headers => true
  pod 'Firebase/Messaging', :modular_headers => true
  pod 'GoogleUtilities', :modular_headers => true
  pod 'FirebaseCoreInternal', :modular_headers => true
  use_react_native!(
    :path => config[:reactNativePath],
    # Enables Flipper.
    #
    # Note that if you have use_frameworks! enabled, Flipper will not work and
    # you should disable the next line.
    #:flipper_configuration => flipper_config,
    # An absolute path to your application root.
    :app_path => "#{Pod::Config.instance.installation_root}/.."
  )

  target 'TaskXSTests' do
    inherit! :complete


  
  
    # Pods for testing
  end

  post_install do |installer|
    # https://github.com/facebook/react-native/blob/main/packages/react-native/scripts/react_native_pods.rb#L197-L202
    react_native_post_install(
      installer,
      config[:reactNativePath],
      :mac_catalyst_enabled => false
    )
  end
end


```ruby
# N/A

AppDelegate.m:

// N/A

#import "AppDelegate.h"
#import <React/RCTBundleURLProvider.h>
#import <Firebase.h>
#import <FirebaseMessaging.h>

@implementation AppDelegate

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    // Configure Firebase
    if ([FIRApp defaultApp] == nil) {
    [FIRApp configure];
    }

    // Set up Firebase Cloud Messaging
    if (@available(iOS 10.0, *)) {
    [UNUserNotificationCenter currentNotificationCenter].delegate = self;
    UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
    [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) {
    if (error != nil) {
    NSLog(@"Error requesting notification authorization: %@", error);
    }
    }];
    } else {
    UIUserNotificationType allNotificationTypes = (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
    [application registerUserNotificationSettings:settings];
    }

    [application registerForRemoteNotifications];

    self.moduleName = @"TaskXS";
    self.initialProps = @{};

    return [super application:application didFinishLaunchingWithOptions:launchOptions];
    }

  • (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
    {
    return [self getBundleURL];
    }

  • (NSURL *)getBundleURL
    {
    #if DEBUG
    return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
    #else
    return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
    #endif
    }

// Implement didRegisterForRemoteNotificationsWithDeviceToken

  • (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
    {
    [FIRMessaging messaging].APNSToken = deviceToken;
    }

// Implement didReceiveRemoteNotification for iOS 10 and above

  • (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
    {
    // Handle notification when app is in the background or inactive
    completionHandler(UIBackgroundFetchResultNewData);
    }

// Implement userNotificationCenter willPresent notification for foreground notification display

  • (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
    {
    NSDictionary *userInfo = notification.request.content.userInfo;

    // Display notification as alert, sound, and badge
    completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionBadge);
    }

// Implement userNotificationCenter didReceive response for background or closed app notification interaction

  • (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
    {
    NSDictionary *userInfo = response.notification.request.content.userInfo;
    // Handle notification when app is in the background or closed
    completionHandler();
    }

@EnD


info.plist file

CFBundleDevelopmentRegion en CFBundleDisplayName XS Project CFBundleExecutable $(EXECUTABLE_NAME) CFBundleIdentifier $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 CFBundleName $(PRODUCT_NAME) CFBundlePackageType APPL CFBundleShortVersionString $(MARKETING_VERSION) CFBundleSignature ???? CFBundleVersion $(CURRENT_PROJECT_VERSION) FirebaseAPIKey AIzaSyDe9iWCR6WrkUgjLSRXEtNPfqKjEInn2i8 FirebaseAppID 1:810581931372:ios:5ea0ad13ca45131fe5ef49 FirebaseMessagingAutoInitEnabled LSRequiresIPhoneOS NSAppTransportSecurity NSAllowsArbitraryLoads NSAllowsLocalNetworking NSCameraUsageDescription $(PRODUCT_NAME) would like to use your camera NSLocationWhenInUseUsageDescription $(PRODUCT_NAME) would like access to your Location NSPhotoLibraryAddUsageDescription $(PRODUCT_NAME) would like to use your photo library NSPhotoLibraryUsageDescription $(PRODUCT_NAME) would like access to your photo gallery UIBackgroundModes fetch processing remote-notification UILaunchStoryboardName LaunchScreen UIRequiredDeviceCapabilities armv7 UIRequiresFullScreen UISupportedInterfaceOrientations UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight UISupportedInterfaceOrientations~ipad UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight UIViewControllerBasedStatusBarAppearance

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

react-native info output:

 OUTPUT GOES HERE
  • Platform that you're experiencing the issue on:

    • [Yes ] iOS
    • Android
    • iOS but have not tested behavior on Android
    • Android but have not tested behavior on iOS
    • Both

    issue only facing in iOS and my android app is working fine

  • react-native-firebase version you're using that has this issue:

    • `e.g@react-native-firebase/messaging": "^19.2.2",
  • Firebase module(s) you're using that has the issue:

    • `e.g. @react-native-firebase/messaging
      @react-native-firebase/app
      @notifee/react-native": "^7.8.2",
  • Are you using TypeScript?

    • Y/N & VERSION
      Y & "typescript": "5.0.4"


Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions