EddyVerbruggen / nativescript-plugin-firebase

:fire: NativeScript plugin for Firebase
https://firebase.google.com
MIT License
1.01k stars 444 forks source link

push notifications not showing up in foreground (android) #1593

Open iampeternguyen opened 4 years ago

iampeternguyen commented 4 years ago

Are there any reasons why push notifications might not show up when the app is in the foreground?

I setup a basic app with the code below. I can get push notifications when I close and kill the app, but when in the foreground I don't get the notification. It does get logged in my console both in the foreground and background though.

firebase
    .init({
        showNotifications: true,
        showNotificationsWhenInForeground: true
    })
    .then(() => {
        console.log("Firebase initialized");
        messaging
            .registerForPushNotifications({
                onPushTokenReceivedCallback: token => {
                    console.log(
                        "Firebase plugin received a push token: " + token
                    );
                },
                onMessageReceivedCallback: message => {
                    console.log("Push message received: ");
                    console.dir(message);
                }
            })
            .then(() => console.log("Registered for push"));
    })
    .catch(error => console.log("Error initializing Firebase: " + error));

from postman

{
    "notification": {
        "title": "My title", 
        "text": "My text", 
        "badge": "1", 
        "sound": "default"
}, 
    "data":{"foo":"bar"}, 
    "priority": "High", 
    "to": "{{DEVICE_KEY}}"
}

in my console log (foreground) JS: ==== object dump start ==== JS: foreground: "true" JS: from: "198270081665" JS: title: "My title" JS: body: "My text" JS: data: { JS: "foo": "bar" JS: } JS: ==== object dump end ====

in my console log (background) - notification received JS: ==== object dump start ==== JS: foreground: "false" JS: data: { JS: "google.delivered_priority": "high", JS: "google.sent_time": {}, JS: "google.ttl": {}, JS: "google.original_priority": "high", JS: "foo": "bar", JS: "google.message_id": "0:1588254508121424%8014bbaa8014bbaa" JS: } JS: google.delivered_priority: "high" JS: google.sent_time: 1588254508106 JS: google.ttl: 2419200 JS: google.original_priority: "high" JS: foo: "bar" JS: google.message_id: "0:1588254508121424%8014bbaa8014bbaa"

ArigarasuthanRepo commented 4 years ago

Use local notification plugin for android only

ale30p commented 4 years ago

same problem

changda0616 commented 4 years ago

Hey there, I am facing the same issue cuz I implemented and extended the FirebaseMessagingService, which I think somehow blocked the thread executing the callback. So maybe you guys can try to find is there any other package implementing the mentioned service like I did. And for now, I am still trying to find the way to implement my own service and make the original service work. One more interesting thing is, I believe the class is not being overridden, cuz when the app is in the background / or closed, the notification from firebase can smoothly arrive. :p

ps: The smoothly arrive which I mean is, I can see the message coming from firebase in the onMessageReceived callback.

b4rtt commented 4 years ago

same problem

Whip commented 3 years ago

Use local notification plugin for android only

@ArigarasuthanRepo can you elaborate on that?

asharghi commented 3 years ago

Use nativescript-local-notifications

And in the callback of nativescript-firebase-plugin's push notification, just create a local notification with forceShowWhenInForeground set to true

private messageReceivedCallback(data) {
            if (isAndroid) {
         LocalNotifications.schedule([
                    {
                        title: "Your title",
                        body: data.data.message,
                        thumbnail: false,
                        forceShowWhenInForeground:  true
                    }
                ]);
            }
}