Skip to content
This repository has been archived by the owner on Sep 24, 2021. It is now read-only.

[iOS] onBackgroundEvent doesn't fire when a local notification is recieved #276

Closed
eqlion opened this issue Mar 31, 2021 · 25 comments
Closed
Labels
blocked: customer-response Waiting for customer response, e.g. more information was requested. Platform: iOS

Comments

@eqlion
Copy link

eqlion commented Mar 31, 2021

In our project we're trying to use Notifee in production to schedule local notifications (without FCM), which when are received, fire some short action in background via onBackgroundEvent. On Android it works somewhat reliably, however on iOS when we received the notification in background nothing fires. I wasn't able to debug it, as there's no logging neither in RN terminal, nor in XCode terminal. If the background task is successful, I would be able to see that, as it fires an external action.

notifee.onBackgroundEvent(async ({ type, detail }) => {
    const { notification, pressAction } = detail;
    console.log(JSON.stringify(detail, null, 2)); // <- this console log doesn't fire at all on iOS while working on Android
    if (type === EventType.DELIVERED) {
       console.log(notification); // <- the same for this console log
        // do something
        }
    } else if (
        type === EventType.ACTION_PRESS &&
        pressAction.id === "mark-as-read"
    ) {
        await notifee.cancelNotification(notification.id);
    }
});

If I try to use onForegroundEvent (just in case), it is fired when a notification is scheduled for some reason.

I've seen #233, but it doesn't really help, as I'm already using the latest version of Notifee, albeit I haven't tried this script.

Versions

iOS: 12.4.5
Android: 10
Notifee: 1.2.1
RN: 0.62.2

@helenaford
Copy link
Member

helenaford commented Mar 31, 2021

Hi, there's an issue (#146) to document that no delivered event will be received for trigger notifications when the app is in the background which is now noted on the website (https://notifee.app/react-native/docs/events).
Does that clarify the issue you're having with the events?

@eqlion
Copy link
Author

eqlion commented Mar 31, 2021

Thank you for quick response, however I can't detect any notification whatsoever. As you can see in the piece of code I've attached to the original message, there're two console.logs, the first one should be triggered on any notification action, while the second one — only for DELIVERED type. However, none of them are triggered (or I can't log it for some reason, in which case could you tell me please how can debug it?)

@helenaford
Copy link
Member

helenaford commented Mar 31, 2021

Hi, if you call displayNotification can you confirm the notification is displayed?

If you press on that same notification while the app is in the background (but the app is still open), does an event comes through? Also, can you put a console.log in the onForegroundEvent to make sure the events aren't getting tracked as foreground ones too?

@helenaford helenaford added the blocked: customer-response Waiting for customer response, e.g. more information was requested. label Mar 31, 2021
@eqlion
Copy link
Author

eqlion commented Apr 1, 2021

@helenaford

  1. displayNotification successfully displays notification (scheduling does so too).
  2. When I press on notification while the app is in the background, the event is caught with onForegroundEvent
  3. When the notification displays while the app is in the background (but still active), nothing logs: neither onForegroundEvent, nor onBackgroundEvent

@helenaford
Copy link
Member

thanks.
For 2. when the app is active or quit. there's an open issue around the press event is in the foreground. Fix is coming up for it.
For 3. are you waiting for the delivered event? this will not happen, as mentioned above.

@eqlion
Copy link
Author

eqlion commented Apr 1, 2021

I'm waiting for at least something, that could trigger my action in background, and I'm not getting anything. This is a crucial part of the project, which works rather well on Android. Is there any way to detect logging while the app is in the background on iOS? As I've mentioned, I couldn't get anything in terminal.

@eqlion
Copy link
Author

eqlion commented Apr 5, 2021

Hello! Any updates?

@helenaford
Copy link
Member

helenaford commented Apr 5, 2021

Hi, I'd recommend tying the demo app here (https://github.com/mikehardy/rnfbdemo) and run ./notifee-demo.sh. Background events do come through for iOs. I'm really not sure why they're not working for you. But, if you want to debug your current app, you could put some break points in here (https://github.com/notifee/react-native-notifee/blob/1da3f3c1e43bea8a85ff02e95e1dbe528d9b6b37/ios/RNNotifee/NotifeeApiModule.m#L73) to see see if events are triggering.

There maybe a TODO item to add some debug logging so when this issue comes up, we can say watch for these in the native logs.

Do your events fire for actions when your phone is in the foreground?

@eqlion
Copy link
Author

eqlion commented Apr 7, 2021

@helenaford I've just used this demo repository, and yes, onForegroundEvent fires there. I might've use onForegroundEvent in the wrong place in my app. However, if I alter the onDisplayNotification function in the following way:

async function onDisplayNotification() {
  // Create a channel
  try {
    const channelId = await notifee.createChannel({
      id: 'default',
      name: 'Default Channel',
    });

    const settings = await notifee.requestPermission();

    if (settings.authorizationStatus === IOSAuthorizationStatus.DENIED) {
      console.log('User denied permissions request');
    } else if (
      settings.authorizationStatus === IOSAuthorizationStatus.AUTHORIZED
    ) {
      console.log('User granted permissions request');
    } else if (
      settings.authorizationStatus === IOSAuthorizationStatus.PROVISIONAL
    ) {
      console.log('User provisionally granted permissions request');
    }

    // Display a notification
    const date = new Date(Date.now());
    date.setMinutes(6, 0); // scheduled to land in ~one minute
    await notifee.createTriggerNotification(
      {
        title: 'Notification Title',
        body: 'Main body content of the notification',
        android: {
          channelId,
          pressAction: {
            id: 'default',
            launchActivity: 'default',
            launchActivityFlags: [AndroidLaunchActivityFlag.SINGLE_TOP],
          },
        },
      },
      {timestamp: date.getTime(), type: TriggerType.TIMESTAMP},
    );
    await notifee.displayNotification();
  } catch (e) {
    console.log('Failed to display notification?', e);
  }
}

Then press on Display Notification button in the app and minimize the app, onBackgroundEvent doesn't log anything whatsoever.

@fciprian42
Copy link

Hi, I have the same issues that @eqlion with IOS onBackgroundEvent which is doesn't work when the screen is lock and you open the notification but call onForegroundEvent ...
On android the backgroundEvent work's perfectly...

@eqlion
Copy link
Author

eqlion commented Apr 7, 2021

@fciprian42 in my case onBackgroundEvent doesn't work even when the screen is on, just the app is minimized (not even fully closed!)

@fciprian42
Copy link

Oh yeah, same than you.
onBackgroundEvent just did not work anyway...

@helenaford
Copy link
Member

helenaford commented Apr 8, 2021

There's a note in the docs that delivered event for iOS in background is not possible. Maybe we need to add that to the typescript docs as well so it doesn't get lost. Does that help at all?

@fciprian42
Copy link

@helenaford did we talk about delivered event ?
No, just about the onBackgroundEvent is not trigger at all, for any event.

@helenaford
Copy link
Member

helenaford commented Apr 8, 2021

Not all events are possible for iOS, and there is an open issue around press action. I can't see in the example above any actions, so there won't be any events. It's important you are as detailed as possible, and describe which events you are trying to listen to.

There's also another git issue (#233 (comment)) where I've pasted the log output of the iOS background event listener.

@eqlion
Copy link
Author

eqlion commented Apr 12, 2021

@helenaford here's my thought process:

  1. notification is somehow scheduled (this part is not a problem, it works nicely)
  2. this notification is shown on the device (while the app is in background, or closed altogether)
  3. at this moment, when the notification is being shown on the device, I wanted to fire some other external action (think fetch some data)
  4. to fire this action I used onBackgroundEvent, that "catches" the received notification, from which I extract the data I need and perform external action.
    This works flawlessly on Android, however this event of receiving/"catching" the notification doesn't work on Android. Shouldn't this still count as an event? Just the notification being received, not pressed, not opened.

@eqlion
Copy link
Author

eqlion commented Apr 15, 2021

Any updates/ideas on how to solve the issue? The core principle of our app revolves around being able to somewhat reliably schedule actions that would be done in background. Without it we can't publish our apps. Any thoughts would be highly appreciated.

@eqlion
Copy link
Author

eqlion commented Apr 21, 2021

Still no updates? We need to provide at least some explanation to our clients and/or solution.

@helenaford
Copy link
Member

Sorry i'm still very confused with what event you require for iOS. it is not possible to know when the trigger notification is displayed/received on iOS.

@eqlion
Copy link
Author

eqlion commented May 11, 2021

@helenaford ok, I see. I've got another question: I know it's off-topic, but I don't know where else to ask — we have a question regarding licensing/payment and the support is ignoring our email for 2 weeks straight now. Is there any other way to get in touch with them? Thank you in advance!

@helenaford
Copy link
Member

helenaford commented May 11, 2021

Yeah if you drop me a dm on Twitter (@helenaellieford) or on the invertase discord channel with your email. I can chase that for you

@eqlion
Copy link
Author

eqlion commented May 11, 2021

@helenaford I can't dm you on twitter (I guess you need to follow me for me to be able to message you) and I can't find discord server address anywhere :(

@helenaford
Copy link
Member

Ok try it now, my dms should be open

@helenaford
Copy link
Member

@eqlion let me know if you can dm me, or you can join the discord with this invite link https://discord.gg/zYvEhf9Wm6

@eqlion
Copy link
Author

eqlion commented May 11, 2021

@helenaford yes, I posted the message at the discord, thank you. I think I'll close the issue, as this is not related anymore.

upd: and I also sent you a pm on twitter, just in case it would be easier for you to find. Thanks again!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
blocked: customer-response Waiting for customer response, e.g. more information was requested. Platform: iOS
Projects
None yet
Development

No branches or pull requests

3 participants