EddyVerbruggen / nativescript-plugin-firebase

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

How to get notification data when app started from notification? #537

Closed erkanarslan closed 6 years ago

erkanarslan commented 6 years ago

I need to know which notification user tapped so that I can show related content when app started. I searched for this but couldn't find a solution. Some of the solutions are specifically for Android. Is there a cross platform Nativescript way for this?

erkanarslan commented 6 years ago

I solved the issue. I thought that onMessageReceivedCallback is called when push message reaches the device. This is true if app is in foreground but not so when it is in background. When app is in background, this method is not called. It will be called when user taps the notification with the notification details like title, body, data, etc.. Also, message parameter passed to onMessageReceivedCallback callback contains a foreground property which shows if app is in background or foreground. It is a boolean.

dvdbrk commented 6 years ago

I am having the same issue.

Foreground notifications works just fine, but after I tap on the notification, the app is launched, but no addOnMessageReceivedCallback / onMessageReceivedCallback is triggered.

Can you please share a snippet of what works for you, when the app is invoked from the notification?

erkanarslan commented 6 years ago
ngOnInit() {

        // ...

    Firebase.init({
        onMessageReceivedCallback : (message : Message) => {
            console.log('Message:');
            console.log('title', message.title);
            console.log('body', message.body);

            // This callback is called when app started with empty values. Check that.
            if(!message.title || !message.body) {
                return;
            }

            if(message.foreground) {
                LocalNotifications.schedule([{
                    title : message.title,
                    body : message.body
                }]);
            }
        },
        onPushTokenReceivedCallback : (token : string) => {
            console.log(token);
            this.fcmToken = token;
        }
    }).then(
        (instance) => {
            console.log('firebase started');
        },
        (error) => console.dir(error)
    );

        // ...
}