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] onBackgroundEvent EventType Press, Dismissed or Delivered doesn't work #1076

Open
shonie2xx opened this issue Aug 5, 2024 · 15 comments
Labels
Keep Open this label avoids the stale bot

Comments

@shonie2xx
Copy link

shonie2xx commented Aug 5, 2024

The onBackgroundEvent works on Android, but not on iOS. The only event type that works for iOS is 7 (TRIGGER_NOTIFICATION_CREATED). Here is how I've implemented it:

const scheduleNotification = async (notificationTimestamp: number) => {
  // Request permissions (required for iOS)
  await notifee.requestPermission();

  // Android 12 limitations
  if (Platform.OS === 'android' && Platform.Version <= 12) {
    const settings = await notifee.getNotificationSettings();
    if (settings.android.alarm !== AndroidNotificationSetting.ENABLED) {
      await notifee.openAlarmPermissionSettings();
    }
  }

  // Create a channel (required for Android)
  const channelId = await notifee.createChannel({
    id: 'reminderId',
    name: 'Reminder',
    vibration: true,
    visibility: AndroidVisibility.PUBLIC,
    importance: AndroidImportance.HIGH,
  });

  // Create a time-based trigger
  const trigger: TimestampTrigger = {
    type: TriggerType.TIMESTAMP,
    timestamp: notificationTimestamp,
  };

  try {
    // Create a trigger notification
    await notifee.createTriggerNotification(
      {
        title: 'Title',
        body: 'Body!',
        android: {
          channelId,
          pressAction: {
            id: 'default',
          },
        },
      },
      trigger,
    );
  } catch (error) {
   recordError(error)
  }
};

In App.tsx I'm expecting the user action this way:

notifee.onBackgroundEvent(async ({ type, detail }) => {

  if (type === EventType.PRESS) {
    console.log('pressed');
  }
  if (type === EventType.DISMISSED) {
    console.log('dismissed');
  }
  if (type === EventType.DELIVERED) {
    console.log('delivered');
  }
});

Please help!

@shonie2xx
Copy link
Author

On [Android], the "press" event type also does not trigger when the phone is locked.

@stephanie-bassey
Copy link

also experiencing a similar issue

@MehmoodArib
Copy link

Any solution here?

@Eclipses-Saros
Copy link

check #616 (comment). looks like onBackgroundEvent is not like what we think about. use onForegroundEvent.

On [Android], the "press" event type also does not trigger when the phone is locked.

check #621 (comment). It's mostly like reactContext is null.

@shonie2xx
Copy link
Author

@Eclipses-Saros Thank you for your response. Using foregroundEvent does not resolve my issue. I still can't capture the event type on iOS. Could you provide more explanation on how to achieve this?

I'm also a bit confused by the documentation. It suggests using onForegroundEvent to handle cases where the user interacts with a notification, but that only applies when the app is in the foreground (visible). In my case, the app is either in the background or the phone is locked. When the user clicks the notification, the app opens, and from what I understand, I need to use onBackgroundEvent to handle the user's actions (events).

@Eclipses-Saros
Copy link

first of all, reactContext is null problem is related to Android. (didn't experienced same bug when iOS)

the app is either in the background or the phone is locked. When the user clicks the notification, the app opens, and from what I understand, I need to use onBackgroundEvent to handle the user's actions (events).

you mean, "the documentation says using onForegroundEvent to catch events, but foreground means app is in the foreground, not background"? yes you're right, but ironically notifee doesn't work what you think of.

NotifeeApiModule.m

- (void)sendNotifeeCoreEvent:(NSDictionary *_Nonnull)eventBody {
  dispatch_after(
      dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (RCTRunningInAppExtension() ||
            [UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
          [self sendEventWithName:kReactNativeNotifeeNotificationBackgroundEvent body:eventBody];
        } else {
          [self sendEventWithName:kReactNativeNotifeeNotificationEvent body:eventBody];
        }
      });
}

here is some code from notifee iOS native module code. when you press notification, native module call sendNotifeeCoreEvent, check whether app status and send event.
put it simply: this code functions as determine app is foreground or not.
there's no more state check function.

in theory, press notification in the background or dead should be recognize as 'background' event... but sadly pressing notification makes app status into foreground, then called native function. that's the why onForegroundEvent function call.

+ however, if you meaning "I can't receive any press events from notification", there are another problems that I didn't know. in that case, I also need investigate this issue further more.

@shonie2xx
Copy link
Author

@Eclipses-Saros thanks for detailed explanation, now I understand. However, my issue is that I'm not receiving any notification press events at all [on iOS, android is working just fine]. Even when the app transitions from background to the foreground, the event doesn't seem to trigger.

Do you have any thoughts on what could be causing this?
I've also assumed that notifee can maybe interfere with other services like react-native-marketingcloudsdk or react-native-firebase/messaging, but that doesn't seem to be the issue.

Copy link

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.

@shonie2xx
Copy link
Author

shonie2xx commented Oct 24, 2024

I think if this PR gets merged, the issue will be resolved.

Copy link

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.

@badhrin
Copy link

badhrin commented Nov 21, 2024

Still an issue. This happens only on IOS & on some Android models (One Plus). It works fine on Samsung & Vivo Android phones.
Issue:

  1. Notifee Display Notification with Quick Actions is shown
  2. If the Quick Action INVOLVES making the App come to the FOREGROUND, then the onBackgroundEvent is not triggered for that ACTION_PRESS event.
  3. If Quick Action does NOT involve the App coming to Foreground, then the onBackgroundEvent is triggered

@mikehardy
Copy link
Collaborator

Indeed - continued appreciation for the patience, I'm through all the react-native new architecture issues, a regression of apple-authentication on iOS simulators and some subtle react-native-firebase issues. Working on Apple dev account provision for a repro + test app here and then this whole thing about iOS handlers is going to get the attention it really needs...

Copy link

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.

@badhrin
Copy link

badhrin commented Dec 20, 2024

Not stale...a lot of us are awaiting a fix. Thanks

@mikehardy
Copy link
Collaborator

Yep, just fixed yet another headlessJS background issue, got auto-publishing working here which really speeds up my development cycle, and finally (!!) got docs for this repo republished including the typedoc reference API just today.

Steady progress in the repo for sure just not quite this issue yet

@mikehardy mikehardy added Keep Open this label avoids the stale bot and removed Type: Stale labels Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Keep Open this label avoids the stale bot
Projects
None yet
Development

No branches or pull requests

6 participants