Comments (23)
@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.
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.
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.
Please try again with the latest alpha
from firebase.
@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.
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.
@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.
@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.
@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.
I just did a quick check, the same problem seems to apply to iOS.
from firebase.
@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.
@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.
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.
@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.
It should I updated the angular demo to test ...
from firebase.
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.
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.
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.
@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.
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.
This shouldn't be an issue again with the latest versions
from firebase.
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.
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)
- Android `showNotificationsWhenInForeground` not working
- Didn't find class "org.nativescript.plugins.firebase.MyFirebaseMessagingService" HOT 1
- Can't build on new xcode 14.3 HOT 5
- Can't build with xcode 14.2 no such module FirebaseMessaging HOT 7
- App crashes on (iOS) device after installing from TestFlight HOT 8
- Nativescript 7.0.0 failed to build plugin @nativescript/firebase HOT 1
- firebase().ui().show crashes on Android with Error: Cannot read properties of undefined (reading 'launch') HOT 1
- @nativescript/firebase-auth and @nativescript/firebase-ui are incompatible on iOS HOT 3
- 'No APNS token specified' error getting token on iOS HOT 20
- [Android] dlopen failed: library "libNativeScript.so" not found HOT 5
- Android warning on App Check HOT 1
- [iOS][auth] Firebase App Check support for Firebase Authentication (Update firebase authentication to >10.8.0)
- With FirebaseAppDelegateProxyEnabled = false, phone authentication hangs and then crashes HOT 1
- `const admin = require('firebase-admin')` crashes with `TypeError: Cannot read properties of undefined (reading 'env')` HOT 3
- Displaying alert after canceling from firebase().ui().show() causes exception HOT 2
- [firebase-crashlytics] Fatal errors aren't sent to Firebase console HOT 2
- [Firebase-storage][Android] Error: An unknown error occurred, please check the HTTP result code and inner exception for server response. HOT 1
- Migrating from legacy FCM APIs to HTTP v1 HOT 3
- [firebase-crashlytics AND firebase-messaging] iOS app crashes upon launch HOT 6
- Expost remove listeners for firebase messaging
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from firebase.