question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Navigate from onNotification when app is killed

See original GitHub issue

Hello @ all,

i’m currently using the package to display local notifications which i receive via Firebase messaging. When a notification arrives in Foreground or background i’ll update my context api and display a local message. To use that i expanded the example NotifService to pass the context.

export default class NotifService {
  constructor(onRegister, onNotification, context) {
    this.context = context;
    this.createDefaultChannels();

    NotificationHandler.attachRegister(onRegister);
    NotificationHandler.attachNotification(onNotification);
    NotificationHandler.attachContext(context); //Added the context to the NotificationHandler

    [...]
  }
class NotificationHandler {
   onNotification(notification) {
    if (notification.userInteraction) {
      if (typeof this._onNotification === 'function') {
        this._onNotification(notification.data);
      }
    } else {
      if (this._context) {
        this._context.addNewNotification(notification.data); //update my context with the notification data
      }
    }
  }

When someone clicks on the notification _onNotification is called on the Context of my HomeScreen and the user is moved to the certain screen.

The NotifService is initialized in my HomeScreen inside App.js after the context is set

static contextType = Context;
  componentDidMount() {
    this.notif = new NotifService(
      this.onRegister.bind(this),
      this.onNotif.bind(this),
      this.context,
    );
    [...]
 }

My problem is, that when the app is killed i just can receive the message via the backgroundMessageHandler of firebase in my index.js.

But at this point i dont have a context. I can initialize a new NotifService without context and the Notification will be shown, but if the notification is pressed context is undefined (because there was no context on initialize) Because of that the onNotif callback is not called and the user is not navigated to the certain screen.

Someone has an idea how i could archive that?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:11

github_iconTop GitHub Comments

2reactions
SteveTrocommented, Dec 6, 2020

But you don’t even try to understand the problem, so you can delete the post anyway.

What I understand is that you are trying to access data provided by a component or component API.

Move your logic outside of the component lifecycle or store the data you need and read it when you need it.

There is many way to do it, but you don’t provide enough information based on your code to help you.

let notif_data;

class NotificationHandler {
   onNotification(notification) {
    if (notification.userInteraction) {
      if (typeof this._onNotification === 'function') {
        this._onNotification(notification.data);
      }
    } else {
      if (this._context) {
        this._context.addNewNotification(notification.data); //update my context with the notification data
      }
      else {
        notif_data = notification.data; // Read it later if needed.
      }
    }
  }
}

OR

const context = new MyContext(); // Load your context here.

class NotificationHandler {
   onNotification(notification) {
    if (notification.userInteraction) {
      if (typeof this._onNotification === 'function') {
        this._onNotification(notification.data);
      }
    } else {
        context.addNewNotification(notification.data); //update my context with the notification data
    }
  }
}

What i try is to archive is to navigate to a certain screen, when a push notification is opend from “killed” state.

My configure() and onNotification is, as you and the example described, outside a component. As the notification is received in killed app state, there is no context in the notification handler. Because of this reason i can’t call “this”, because the context is undefined in killed state.

I already tried few ways, but i cant archive it.

0reactions
skkim-pixel-monstercommented, Sep 28, 2021

@jailsonpaca Linking.openUrl does not work in for not-foreground notifications Could you please share detailed code or settings?

Read more comments on GitHub >

github_iconTop Results From Across the Web

FCM Navigation not working while app was killed or ...
onMessageReceived will not get triggered if the app is killed or in background.If your app is in the background or closed then a...
Read more >
Push notification doesn't open specific screen when app is ...
I'm using addNotificationResponseRecievedListener in useEffect hook where I navigate to specific screen when I have a response.
Read more >
FCM notification not received when app is terminated
FCM notification not received when app is terminated. I'd like to ask a question regarding Firebase Cloud Messaging.
Read more >
Flutter: FCM — How to Navigate to a Particular Screen After ...
In this post, I'll go over how to receive FCM Push Notifications and then push ... receive a notification while the app is...
Read more >
awesome_notifications | Flutter Package - Pub.dev
Notifications could be created at any moment (on Foreground, Background or even when the application is terminated/killed). High trustworthy on receive ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found