Comments (15)
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.
@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.
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.
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.
@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.
Yup Same problem here
from react-native-notifications.
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.
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.
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.
done same as @andreipham told but still not getting notification when app is closed.
from react-native-notifications.
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.
@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.
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.
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.
Nothing.
from react-native-notifications.
Related Issues (20)
- Invariant Violation: new NativeEventEmitter() requires a non-null argument.
- Update Robolectric version
- Subscriptions to topics
- "$" getting copied HOT 1
- Code that setting NotificationChannel sound on Android might lead unexpected consequences HOT 1
- Application does not trigger registerNotificationOpened when it is closed, only when it is in the background.
- I can't get notifications to open my app HOT 1
- How to undo `Notifications.registerRemoteNotifications()` ? HOT 3
- Minification failure with R8: Compilation failed for ReactAppLifecycleFacade$1 in react-native-notifications
- Not receiving any notification on iOS release mode only HOT 1
- Huawei push notification in library
- dexOptions is obsolete
- Feature request: add support for iOS simulator push notifications HOT 3
- Does FCM work with this package in iOS? HOT 4
- Handle callback when notification in background / terminate app HOT 3
- When I click on the notification on my android app, entire app reloads HOT 4
- Question - is there support for the new RN architecture? HOT 1
- Suggestion - documentation update HOT 1
- Feature request: support for dynamic Live Activities on IOS
- What about the wiki for integration with react notification?
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 react-native-notifications.