Giter Club home page Giter Club logo

Comments (23)

williamjuan027 avatar williamjuan027 commented on June 15, 2024 2

@triniwiz When I test this on Android, I found out that if I add the onNotificationTap handler early enough (like in the main.ts), it does get triggered when the app is closed, but if I set it late (like in a service), it only gets called when the app is in the background.

// main.ts

import { firebase } from '@nativescript/firebase-core';
import '@nativescript/firebase-messaging';

firebase().initializeApp().then(app =>{
      firebase().messaging().onNotificationTap((message) => {
        // gets called when tapping on the notification while the app is closed, but not when the app is in the background
        console.log('[Main][onNotificationTap]', message);
      });
  });
// messaging.service.ts

import { Injectable } from '@angular/core';
import { firebase } from '@nativescript/firebase-core';

@Injectable({
    providedIn: 'root'
})
export class MessagingService {

    init(): void {
        firebase().messaging()
        .requestPermission()
        .then(() => {
            firebase().messaging().onMessage((message) => {
                // gets called when an fcm is received while app is in the foreground
                console.log('[MessagingService][onMessage]', message);
            });
            
            firebase().messaging().onNotificationTap((message) => {
                // gets called when a notification tap while app is in the background
                console.log('[MessagingService][onNotificationTap]', message);
            });
        })
    }

}

Hope this helps :)

from firebase.

jessorlisa avatar jessorlisa commented on June 15, 2024 1

Sorry for the delay:

Setup 1: Angular project, handlers added in a service

  • fcm message received while in foreground: onMessage (Android/iOS)
  • notification tapped while in background: onNotificationTap (Android/iOS)
  • notification tapped while closed: none (Android/iOS)

Setup 2: Angular project, handlers added in main.ts before bootstrap

  • fcm message received while in foreground: onMessage (Android/iOS)
  • notification tapped while in background: onNotificationTap (Android/iOS)
  • notification tapped while closed: onNotificationTap (Android) / none (iOS)

I also noticed that on iOS you have to register the device for remote messages after requesting permission, otherwise it does not work at all. Can anybody confirm this?

firebase().messaging()
    .requestPermission()
    .then(() => {

        // seems to be necessary for iOS to work?
        if (isIOS) {
            firebase().messaging()
                .registerDeviceForRemoteMessages()
                .catch((e) => {
                    console.error(e);
                });
        }
    });

from firebase.

jessorlisa avatar jessorlisa commented on June 15, 2024 1

@triniwiz

I set up a simple demo project to show the current issue:
https://github.com/jessorlisa/demo-issues-nativescript-ng/tree/issue/firebase-messaging

All relevant information can be found in the "Issue description" and "Reproduction" section.

Let me know if

  • you need anything else from me to check this
  • I should open a new issue instead on commenting on this one ;)

On a side note:
This repo's angular demo project is failing with some firebase-admob related errors
ERROR in ../../packages/firebase-admob/utils.ts:7:31 - error TS2552: Cannot find name 'GADRequest'. Did you mean 'IDBRequest'?

from firebase.

triniwiz avatar triniwiz commented on June 15, 2024

Please try again with the latest alpha

from firebase.

jessorlisa avatar jessorlisa commented on June 15, 2024

@triniwiz Just tried with 1.0.0-alpha.42 and I am afraid onNotificationTap is still not called on app launch (when closed).

@nikoTM Does is work for you?

from firebase.

jessorlisa avatar jessorlisa commented on June 15, 2024

I just found this, is this the recommended way for the Angular flavor?

Originally posted by @williamjuan027 in #24 (comment)

@triniwiz It turns out to be the initialization issue. In an Angular project, initializing it in the main.ts resolves the issue. Below is the code I used to initialize the plugin.

// main.ts

import { firebase } from '@nativescript/firebase-core';
import '@nativescript/firebase-messaging';

const firebaseInit = firebase().initializeApp();
if (firebaseInit) {
  firebase().messaging();
}

from firebase.

williamjuan027 avatar williamjuan027 commented on June 15, 2024

@jessorlisa There was a recent update that made the initializeApp() function return a Promise, so the following should be enough to initialize the plugin.

// main.ts

import { firebase } from '@nativescript/firebase-core';
import '@nativescript/firebase-messaging';

firebase().initializeApp().then((app) => {
   const firebaseApp = app;  // optional - if you need reference to the firebase app :) 
});

from firebase.

jessorlisa avatar jessorlisa commented on June 15, 2024

@williamjuan027 Thanks a lot. I just realized that studying the source. 😊

However moving the initialization to main.ts does not solve the problem of onNotificationTap not being called when the app was closed. (Which was what I had hoped for when reading your comment in the linked ticket. 😞 )

from firebase.

williamjuan027 avatar williamjuan027 commented on June 15, 2024

@jessorlisa Yes, you're right. I just tested that out on my end, and onNotificationTap seems to only get called when the app is in the background and not when its closed.

from firebase.

jessorlisa avatar jessorlisa commented on June 15, 2024

@triniwiz @williamjuan027

I just did a quick check, the same problem seems to apply to iOS.

from firebase.

williamjuan027 avatar williamjuan027 commented on June 15, 2024

@jessorlisa I was seeing it come in under onMessage for iOS when you tap on a notification while the app is closed and onNotificationTap when the app is in the background. Are you seeing the same thing on your end?

from firebase.

jessorlisa avatar jessorlisa commented on June 15, 2024

@williamjuan027 None is called - but maybe because I currently add the handlers in a service, not the main.ts. Will do another test tomorrow.

from firebase.

fpaaske avatar fpaaske commented on June 15, 2024

I also noticed that on iOS you have to register the device for remote messages after requesting permission, otherwise it does not work at all. Can anybody confirm this?

I can confirm. FCM was not working until I added this call. It's also mentioned in #24. Spent way too long to discover this 😄

from firebase.

jessorlisa avatar jessorlisa commented on June 15, 2024

@triniwiz Is the 3rd scenario as mentioned below supposed to work with the latest version (1.1.2)?

Setup 1: Angular project, handlers added in a service

  • fcm message received while in foreground: onMessage (Android/iOS)
  • notification tapped while in background: onNotificationTap (Android/iOS)
  • notification tapped while closed: none (Android/iOS)

As of right now I am still having issues in that case.

from firebase.

triniwiz avatar triniwiz commented on June 15, 2024

It should I updated the angular demo to test ...

from firebase.

jessorlisa avatar jessorlisa commented on June 15, 2024

I followed your demo exactly, still not working. No handler is called if the app is closed and the handlers are added in a service. Looking at the commit message It seems only the missing onNotificationTap for iOS as mentioned in setup 2 was addressed?

Setup 2: Angular project, handlers added in main.ts before bootstrap

  • fcm message received while in foreground: onMessage (Android/iOS)
  • notification tapped while in background: onNotificationTap (Android/iOS)
  • notification tapped while closed: onNotificationTap (Android) / none (iOS)

But I will do another check ...

from firebase.

jessorlisa avatar jessorlisa commented on June 15, 2024

I am afraid I have the same situation as back in February. I tried to follow the demo exactly.
Dummy question: How can I run the demos part of this repo? 😊

from firebase.

triniwiz avatar triniwiz commented on June 15, 2024

Try the following

git clone https://github.com/NativeScript/firebase
cd firebase
npm run setup
npm run start // you can choose one of the demos

from firebase.

13dante04 avatar 13dante04 commented on June 15, 2024

@triniwiz Could you reopen this, me and @jessorlisa are having this issue.
I'm currently using

 "@nativescript/angular": "13.0.3",
    "@nativescript/core": "~8.2.1",
...
 "@nativescript/firebase-analytics": "^1.2.0",
    "@nativescript/firebase-core": "^1.2.0",
    "@nativescript/firebase-crashlytics": "^1.2.0",
    "@nativescript/firebase-dynamic-links": "^1.2.0",
    "@nativescript/firebase-messaging": "^1.2.0",

This seems to be an issue with Angular only since no one else is reporting it, the demo project is actually the same issue I'm getting.

from firebase.

nikoTM avatar nikoTM commented on June 15, 2024

Hey @triniwiz, I have read the thread here, but I am unsure what is the solution supposed to be. Notification handlers are still not being invoked if they are registered within an Angular service. Moving them to main.ts does not make sense, since it is essentially moves it out of the app context, making it difficult to handle notifications within the app without doing backward bending.

from firebase.

triniwiz avatar triniwiz commented on June 15, 2024

This shouldn't be an issue again with the latest versions

from firebase.

nikoTM avatar nikoTM commented on June 15, 2024

It's still the happening for me on:

├── @nativescript/[email protected]
├── @nativescript/[email protected]

On android when the app is closed, notification tap will not invoke the callback, unless I move out MessagingCore.getInstance().addOnNotificationTap... out of a service.

Update:

Seems to be working with

firebase()
  .initializeApp()
  .then(async () => {
    await MessagingCore.getInstance().registerDeviceForRemoteMessages();
  });

in main.ts

from firebase.

MarkOdey avatar MarkOdey commented on June 15, 2024
I am having the same issue as @jessorlisa. In android while app is totally closed not just in background. The listener onNotificationTap is not catching the event. 

No problem while the app is in background.

I am using :

- nativescript-vue 2.9.3 
- @nativescript/core 8.5.9
- @nativescript/firebase-messaging 3.2.0  

Finally, firebase was instantiated to late. I initialized firebase before Vue and also made sure that

await MessagingCore.getInstance().registerDeviceForRemoteMessages();

was wrapped in a then promise pattern.

It all works now.

from firebase.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.