Giter Site home page Giter Site logo

Comments (15)

andrei-pham avatar andrei-pham commented on July 24, 2024 3

Ok, so the solution to this specific problem is to send both data and notification objects within the payload. If you want to send along some custom data, just append it to the data object, like you normally would. I suspect this is because when in background, Gcm automatically handles and displays the notification, while when the app is in foreground, react-native-notifications handles the notification.

This is how the payload should look:

{
    "notification": {
            "priority" : "high",
            "sound" : "default",
            "tag": "example",
            "icon" : "icon",
            "title" : "Notification Title",
            "body" : "Notification Body"
    },
    "data": {
            "priority" : "high",
            "sound" : "default",
            "tag": "example",
            "icon" : "icon",
            "title" : "Notification Title",
            "body" : "Notification Body",
            "custom_field" : {}
    },
    "registration_ids" : [
            "dikzJkIhCPs...TPA9"
    ]
}

This solves the problem mentioned at the beginning.

If you were to send only the notification object, you would have the following issues:

  • The notification would be empty when received in foreground
  • The notification would be ok when received in background, but it wouldn't trigger sound/vibration

If you were to send only the data object containing the notification fields:

  • The notification would be empty when received in background
  • The notification would be ok when received in foreground

Therefore, the only solution that I had found was to send the notification fields within both of these objects in the payload.

However, applying this would introduce the problem of #38, whose root cause was exactly the fact that he sent both data and notification with the payload, which in this case is the solution.

from react-native-notifications.

andrei-pham avatar andrei-pham commented on July 24, 2024 2

@d4vidi From what I understand, the problem from #38 was the fact that both data and notification were sent along with the request, while the solution was sending only the data object with all the notification options inside it.

In my case, I only send the notification payload, without data. So I don't think I have the same problem. If I apply the suggested changes and use data instead of notification, the device obviously doesn't display any kind of notification at all. GCM's docs even state that the client is responsible for processing data messages, while the notification messages are automatically displayed as system notifications.

Can you please give me an example of a payload that you would normally send to gcm, which successfully triggers notifications? Perhaps something is wrong with mine.

from react-native-notifications.

 avatar commented on July 24, 2024 2

Send only data message and use below code in FCM onMessageReceived() method.

PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();
Log.e("screen on.................................", ""+isScreenOn);
if(isScreenOn==false)
{
WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE,"MyLock");
wl.acquire(10000);
WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MyCpuLock");

wl_cpu.acquire(10000);

}

from react-native-notifications.

d4vidi avatar d4vidi commented on July 24, 2024 1

Is it possible that your devices are in Doze mode when this takes place? Try to compare your behavior with other (low-priority, e.g. not WhatsApp) notifications.

from react-native-notifications.

d4vidi avatar d4vidi commented on July 24, 2024 1

@arnebr @andreipham Please have a look at issue #38 (more specifically, refer to this link: https://developers.google.com/cloud-messaging/concept-options#target)

from react-native-notifications.

arnebr avatar arnebr commented on July 24, 2024

Yup Same problem here

from react-native-notifications.

andrei-pham avatar andrei-pham commented on July 24, 2024

No, I'm running my app on Android 5.0.2, and since Doze mode was introduced in 6.0, that cannot be the case. Nevertheless, I do receive notifications from WhatsApp or other apps even with my device locked. edit: Even if they are low priority, I still receive them.
I'll try to tweak with the payload or the receiver.

However, have you got any idea as to why do the notifications appear as completely empty (except for the icon) when they are received while the app is in foreground?

Thanks

from react-native-notifications.

andrei-pham avatar andrei-pham commented on July 24, 2024

I have also noticed that if I close the app, the notifications won't arrive at all. Is there something I have to do or is it related to your background listener?

from react-native-notifications.

arnebr avatar arnebr commented on July 24, 2024

When I have the App opened the log successfully put out:
'Notification received on device' - with the correct Data but the Notification is empty except for the ic_launcher Icon when clicked setNotificationOpenedListener is firing.
I get the following error in the log:

D/GcmReceiver: Missing wake lock permission, service start may be delayed

03-10 10:59:53.195 10660-10766/com.fvjm.jphh W/Bundle: Key google.sent_time expected String but value was a java.lang.Long.  The default value <null> was returned.
03-10 10:59:53.195 10660-10766/com.fvjm.jphh W/Bundle: Attempt to cast generated internal exception:
                                                       java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String
                                                           at android.os.Bundle.getString(Bundle.java:1121)
                                                           at com.google.android.gms.gcm.zza.zzF(Unknown Source)
                                                           at com.google.android.gms.gcm.GcmListenerService.zzo(Unknown Source)
                                                           at com.google.android.gms.gcm.GcmListenerService.zzn(Unknown Source)
                                                           at com.google.android.gms.gcm.GcmListenerService.zzm(Unknown Source)
                                                           at com.google.android.gms.gcm.GcmListenerService.zza(Unknown Source)
                                                           at com.google.android.gms.gcm.GcmListenerService$1.run(Unknown Source)
                                                           at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                           at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                           at java.lang.Thread.run(Thread.java:841)

When the app process is dead the device receives the notification with content but no action is logged when clicked even when the setNotificationOpenedListener is set.

from react-native-notifications.

developeromI89 avatar developeromI89 commented on July 24, 2024

done same as @andreipham told but still not getting notification when app is closed.

from react-native-notifications.

kholiavko-roman avatar kholiavko-roman commented on July 24, 2024

How to trigger callback if app was dropped from process on android ?

I tried different scenarios, only with data, with notification, with both, but nothing trigger callback if app killed from process. Anybody found the solution ?

from react-native-notifications.

developeromI89 avatar developeromI89 commented on July 24, 2024

@kholiavko-roman If app is killed from process it means nothing will happen.It should be in memory or in background then it will work.

from react-native-notifications.

kholiavko-roman avatar kholiavko-roman commented on July 24, 2024

But usually apps has possibility to run some code on push notificaiton, for example I need increase badge counter (small icon on app icon with count of new notificaion) on my app, on android.

For exaple react-native-fcm has this functionality, but I trying this plugin because I am using react-native-navigation by wix, and when app push arrives, when app killed, app started to foreground. Here is problem: https://github.com/wix/react-native-navigation/issues/1799

from react-native-notifications.

ShongSu avatar ShongSu commented on July 24, 2024

Same issue. When app in background, I can receive notification and bring my app to foreground, but if I killed the app, nothing happened when I sending notification to the device. Any solution so far?

from react-native-notifications.

kholiavko-roman avatar kholiavko-roman commented on July 24, 2024

Nothing.

from react-native-notifications.

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.