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

[iOS] onForegroundEvent is not getting triggered when clicking background notifications #1155

Open
cw-riteshgoyal opened this issue Dec 7, 2024 · 1 comment

Comments

@cw-riteshgoyal
Copy link

I have a notification extension service integrated, and I am successfully receiving notifications in all three states: foreground, background, and killed. When I click on a notification in the foreground, the onForegroundEvent is triggered as expected. However, in the other two states (background and killed), this is not working. While didReceiveNotificationRequest works, setBackgroundMessageHandler is not functioning on iOS. I believe this might be the intended behavior of iOS, as the app is not supposed to work in the background state, but i am confused why onForegroundEvent is not working on clicking on background notifications. (I know onBackgroundEvent is not supposed to work in iOS)

Postman Request :

{
   "message":{
      "token":"token",
      "notification":{
         "title":"Title",
         "body":"Body",
         "image": "image"
      },
      "data":{
         "title":"Title",
         "description":"Description"  
      },
      "apns": {
        "payload": {
            "aps": {
                "contentAvailable":1,
                "mutableContent": 1
                }
            }
        }
   }
}

NotificationService.m :

//
//  NotificationService.m
//  NotificationServiceExtension
//
//  
//

#import "NotificationService.h"
#import "NotifeeExtensionHelper.h"
@interface NotificationService ()

@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;

@end

@implementation NotificationService

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
    
    // Modify the notification content here...
  
  NSLog(@"Sample Message");

  [NotifeeExtensionHelper populateNotificationContent:request
                                  withContent: self.bestAttemptContent
                                  withContentHandler:contentHandler];
}

- (void)serviceExtensionTimeWillExpire {
    // Called just before the extension will be terminated by the system.
    // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
    self.contentHandler(self.bestAttemptContent);
}

@end

index.js :

/**
 * @format
 */
import messaging from '@react-native-firebase/messaging';
import { AppRegistry } from 'react-native';
import App from './src/App';
import { name as appName } from './app.json';
import FirebaseAnalyticsClient from 'src/l2r_common/utils/firebaseAnalyticsClient';
import notifee, { EventType } from '@notifee/react-native';
import { useEffect } from 'react';
  
messaging().onMessage(async remoteMessage => {
    console.log('A new FCM message arrived!', JSON.stringify(remoteMessage));
    notifee.displayNotification({
        title: remoteMessage.data?.title,
        body: remoteMessage.data?.description,
        android: {
            channelId: 'default',
            pressAction: {
                id: 'default',
            },
        },
        data: remoteMessage
    });
});

messaging().setBackgroundMessageHandler(async remoteMessage => {
    console.log('Message handled in the background!', remoteMessage);
    notifee.displayNotification({
        title: remoteMessage.data?.title,
        body: remoteMessage.data?.description,
        android: {
            channelId: 'default',
            pressAction: {
                id: 'default',
            },
        },
        data: remoteMessage
    });
});

notifee.onForegroundEvent(async ({ type, detail, headless }) => {
    console.log(detail)
});

notifee.onBackgroundEvent(async ({ type, detail, headless }) => {
    console.log(detail)
});


AppRegistry.registerComponent(appName, () => App);

Environment :

react-native: 0.72.17
@notifee/react-native: 9.1.3
react: 18.2.0
@react-native-firebase/messaging: 20.3.0

iOS Deployment Target: 12.4
Tested iPhone Device: iPhone 15 Pro (Emulator) (iOS 17.0)
@andriibodryi
Copy link

same issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants