push-plugin icon indicating copy to clipboard operation
push-plugin copied to clipboard

Android: Not getting push notification on background

Open mayureshjadhav opened this issue 6 years ago • 11 comments

I am using v 0.3.0. I am getting a notification when the app is running but not working while the app is not running.

mayureshjadhav avatar Jan 06 '18 07:01 mayureshjadhav

Can you share some more details about this issue:

  • are you able to reproduce it using the demo app?
  • what version of Android (device or emulator) are you testing with?
  • what is the content of the notification you are trying to send?

lini avatar Jan 08 '18 07:01 lini

@lini I am sending following content

{"to":"[add your token]","data":{"title":"Test Title","body":"Test Description"},"priority":"high"}

and on notification receive, I am using nativescript-local-notifications plugin to show the notification

 notificationCallbackAndroid: (stringifiedData: string, fcmNotification: any) => {
                console.log("String Data : " + stringifiedData);
                console.log("FCM Notification : " + JSON.stringify(fcmNotification)); 
                let notification = JSON.parse(stringifiedData);
                LocalNotifications.schedule([{
                                    id: 1,
                                    title: notification.title,
                                    body: notification.body,
                                    ticker: 'The ticker',
                                    badge: 1,
                                    at: new Date(new Date().getTime() + (10 * 1000)) // 10 seconds from now
                                }]).then(
                                    () => {
                                        console.log("Notification scheduled");
                                    },
                                    (error) => {
                                        console.log("scheduling error: " + error);
                                    }
                                    );
}

When the app is running I am receiving the notification but when the app is not working I am not getting any notification.

I am using Moto G device with 5.1 Android version to test this functionality.

mayureshjadhav avatar Jan 08 '18 08:01 mayureshjadhav

as far as i know if you dont send notification part in your json it wont shown when app in background. Because default behavior for plugin when app in background is need notification part in your json.i dont know if you change this. but if you change your json like this; {"to":"[add your token]", "data":{"title":"Test Title","body":"Test Description"}, "notification": { "title": "Test Title", "text": "Test Description" }, "priority":"high"}

it should work both foreground and background

tomylee001001 avatar Jan 09 '18 08:01 tomylee001001

@tomylee001001 is correct. But I'm sure it was working before with only sending data in both foreground and background (was using v0.1.2). Not sure if this is because the plugin uses some new version of the native libs or because some change in the plugin, but it should be noted somewhere in the readme/breaking changes for the version.

PeterStaev avatar Jan 12 '18 14:01 PeterStaev

After digging the code for another problem, seems issue came with the removal of the NotificationBuilder class which was pulling the title/message from the data https://github.com/NativeScript/push-plugin/blob/0.1.2/native-src/android/app/src/main/java/com/telerik/pushplugin/NotificationBuilder.java#L77 https://github.com/NativeScript/push-plugin/blob/0.1.2/native-src/android/app/src/main/java/com/telerik/pushplugin/NotificationBuilder.java#L83-L88

With the removal now we have to use the notification structure. Again this should have been in the Breaking Changes for v0.2 👎

PeterStaev avatar Jan 12 '18 14:01 PeterStaev

Still not working

mayureshjadhav avatar Jan 15 '18 07:01 mayureshjadhav

Is there any updates on it?

mayureshjadhav avatar Jan 19 '18 10:01 mayureshjadhav

@mayureshjadhav I'm not able to reproduce this issue - With {notification: {text: "Something"}} I'm able to get notifications both in foreground and background.

@PeterStaev This was I believe a result of the change from GCM to FCM.

ghost avatar Feb 19 '18 12:02 ghost

@naiveaiguy It's not working. My scenario is, I am sending following data from server {"to":"[add your token]","data":{"title":"Test Title","body":"Test Description"},"priority":"high"} but I am not able to receive it. If I send {"to":"[add your token]","notification":{"title":"Test Title","body":"Test Description"},"priority":"high"} It is working but It shows me the notificatio. I don't want to show the notification. I wanted to pass the data silently without showing it to user.

mayureshjadhav avatar Feb 28 '18 06:02 mayureshjadhav

@lini I am getting the same behavior as OP. Using data only notifications I get the notificationCallback() when my app is in the foreground or minimized and in the recent used tray. When I swipe the app away, I see that my data-notifications are cached because no callback was found . Is this the intended behavior?

When using the android sdk from firebase Quickstart demo, my messageReceivedCallback() gets called even when I swipe the app whereas when I use the NativeScript plugin it doesn't get called unless I relaunch the app.

This was tested on multiple devices (s8 API 26, pixel, Moto API 24, iPhone X and 6s) using the demo app. I have noticed this in https://github.com/EddyVerbruggen/nativescript-plugin-firebase as well. Is it not possible to replicate the behavior in the FCM Quickstart demos?

KkevinLi avatar Jun 26 '18 17:06 KkevinLi

Just leaving another bump. I'm using an older GCM library for sending (I really don't want to migrate at this point in time). So no notification structure means i only get the message from within the app.

Also the message on Android is a string (json encoded) while on ios it's a json object.

Any plans to fix these or should i look into it myself or get another plugin?

michael-dots avatar Sep 18 '18 07:09 michael-dots