-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
onNotification is not calling when app is in background #1431
Comments
Hi @rizwanshaikh199091 |
Hi @Dallas62 Thanks for quick reply, I put my code inside constructor function. This is the code that I put inside constructor function of my call and calling api from postman. PushNotification.configure({
// (optional) Called when Token is generated (iOS and Android)
onRegister: function (token) {
console.log("TOKEN:", token);
thats.setState({deviceToken:token});
},
// (required) Called when a remote or local notification is opened or received
onNotification: function (notification) {
console.log('** NOTIFICATION: **', notification);
if (Platform.OS === 'ios') {
if (notification.alert.length !== 0) {
//handleNotification(notification)
notification.finish(PushNotificationIOS.FetchResult.NoData);
}
} else {
//handleNotification(notification)
}
},
// IOS ONLY (optional): default: all - Permissions to register.
permissions: {
alert: true,
badge: true,
sound: true,
},
// Should the initial notification be popped automatically
// default: true
popInitialNotification: true,
requestPermissions: true,
});
PushNotification.popInitialNotification((notification) => {
console.log(notification);
}) I have attached a few screenshots taken in both the conditions. Please have a look. Please advice. Thanks |
I think you should put the |
Okay let me try this |
I'm having the same issue. Tried it both in componentDidMount and outside of a component. Same issue. |
I didn't notice, but in your screenshot there is the https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages
I know this is not what you expect, but I don't think we can change this. |
Same issue here, I tried with the example app and onNotification is always called except when app is in backgroud. |
Ok, i figured it out: the problem is that onNewIntent() is not called on ReactContextBaseJavaModule I modified
|
@taymer This appears to be a source of crashes now. I've received the following stacktrace a number of times over the weekend (Release build). I assume this means
|
Hi @wbercx |
@Dallas62 I do not remember the exact reason why though. I feel like I've had to mess with that a few times over the years. I wrote a particularly useless commit message for that too... EDIT: public RNPushNotification(ReactApplicationContext reactContext) {
super(reactContext);
reactContext.addActivityEventListener(this);
+ // This is used to delivery callbacks to JS. Instantiated before registering activityLifeCycleCallbacks.
+ mJsDelivery = new RNPushNotificationJsDelivery(reactContext);
Application applicationContext = (Application) reactContext.getApplicationContext();
applicationContext.registerActivityLifecycleCallbacks(this);
// The @ReactNative methods use this
mRNPushNotificationHelper = new RNPushNotificationHelper(applicationContext);
- // This is used to delivery callbacks to JS
- mJsDelivery = new RNPushNotificationJsDelivery(reactContext);
mRNPushNotificationHelper.checkOrCreateDefaultChannel();
} |
@wbercx |
@Dallas62 Have a look at the above, I edited it just moments earlier. |
@wbercx This is not a good solution anyway, this cause a bug on background by triggering the notification again and again when you switch from background / foreground state. #1455 |
Is this fixed? It's not working for me either. |
Hi @AndersonSchmidt |
@Dallas62 First of all, thank you for your response. I already tried to do it as the image shows. It only calls onNotification if the app is in Foreground. If I exit or navigate to another app, I receive the notification but onNotification is not called. I'm using version 3.5.1. I don't know what to do anymore, I spent all day trying to fix this. I would be really glad if you have any solution for me. Thanks. |
In another issue: |
@Dallas62 what version of android is your device ? I've still the same issue with 7.0 |
@taymer But didn't you manage to solve it? Your solution was even merged, but I saw that it was removed on version 3.5.2. By the way, I tested on android 8 and 10 and it's not working. |
@AndersonSchmidt I've tried 3.5.2, but I'm still using my piece of code, which works with the app in background, but not if it's closed (it calls onNotification only if you put the app in background and then again in foreground) . Sadly for now I've not enough time to check this issue. |
@taymer I understand. Do you have any tips/suggestions for me? I need to call onNotification ALWAYS when a notification is received to update a redux state to add a notification badge number on my hamburger button, so the user will know if and how many notifications he has. I was progressing well but got stuck on this. Check the badge I'm using. |
@AndersonSchmidt I've just tried the example app with version 3.5.2 and it works like a charm, so the problem is in our use of the library |
I will retest this at the end of the day. |
Also check the |
@Dallas62 In the example inside the library the launch mode is |
@Dallas62 thanks for that, now I am getting notification response in onNotification callback, but when I click notification "userInteraction" key is not present in quit state , and I am not able to perform any operation on it. I need to perform some operation when click on notification.so for that what should I do . can you help me on that? |
I am still facing this problem. can you give some example code? Thank you |
I've followed this process. I sent data-only and trigger a local notification. But when I press the notification it will trigger another local notification. Can you please give an example on how you implement receiving remote notifications and trigger local notification. |
Hi @romelbonnie |
Oh I see. Thank you @Dallas62 |
@Dallas62 but i see "console.log('TOKEN:', token);" in debugger and just it please help me !!! |
Hi, There is no channelId defined. |
Tnx Man.
Tnx Man |
@Dallas62 I use this AndroidManifest please help me, |
Hi @rizwanshaikh199091 Refer to: Regards, |
* commit '1d497e60a142b4a846907ec19b80ab72a5ae5c8d': (109 commits) Bump to 3.5.2. Remove extra interface. Fix sounds volume and DND is ignored. Fix zo0r#1455 `onNotification fires every time when the app goes from background to foreground` Update README.md Bump to 3.5.1. Switch from support library to androidx. zo0r#1449 Bump to 3.5.0. Move back onNewToken to Firebase service. zo0r#1431 feat: explicitly override onNewToken Change from 18 months to 1 year feat: multiple push providers Missing change on variable name. Prevent sound to be mute in default state. Update CHANGELOG.md. Prevent crash for NaN. Bump to version 3.4.0. Put deleteInstanceId() in a separate thread. Use directly the callNative for `abandonPermissions`. Changes in example Application. ... # Conflicts: # android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationListenerService.java
Hey I found this issue in Ios side |
I am sending a data notification and the onNotification function is not called when the app is in the background. But it is called when the application is in the foreground. {
"to" : "fXJ_A95kTQOqm6koxOFBwh:APA91bEAUKYfn4dUhxa00yKNzs4F0b3nAh8oxXGC3cegOxhI2kCQN0XbZ0elI9M640W4SYllNF_rHQOWm8V1ryaueUN6h9ddfX2n9xfQplAzzRzBxVqDdyLa0Yl7NjigsZtlDqTeVS1i",
"data" : {
"initiatorId" : "127167807"
},
"priority": "high"
} |
hey @stephanoparaskeva |
Hi @stephanoparaskeva In general, do not put |
@Dallas62 const onRegister = ({ token }: TDeviceInfo) => AsyncStorage.setItem(FCM_OR_APNS_TOKEN, token);
PushNotification.configure({
onRegister,
onNotification,
popInitialNotification: true,
requestPermissions: true,
});
const findTokenEvent = (evt: TEvent) =>
evt.name === VoipPushNotification.RNVoipPushRemoteNotificationsRegisteredEvent;
VoipPushNotification.addEventListener('didLoadWithEvents', (events: Array<TEvent>) => {
const apnsVoipToken = events.find(findTokenEvent)?.data;
AsyncStorage.setItem(APNS_VOIP_TOKEN, apnsVoipToken || '');
});
AppRegistry.registerComponent(name, () => app); This is my androidmanifest.xml file: I think it should be correct? |
@stephanoparaskeva Since there is also, another library which can intercept you code, you can check logs with: Regards, |
I commented out the QB_PUSH_NOTIFICATION_CHANNEL so it should work as default right? Here is my updated androidManifest.xml What should I look for in logcat? |
@Dallas62 It works for me on IOS but not on android in the background. This is the request I make on IOS and it works: lola \
-bundleId com.euclid \
-device 35214b2782c4445231d7512dfa2bfed89b9fcde32d284e1acd5633d631c4ef2f \
-teamId HLR7ND6WN5 \
-authKey AuthKey_SHVZ2CXB5P.p8 \
-notificationType background \
-json "{ \"aps\": { \"content-available\": 1 }, \"initiatorId\": 127167807 }" But this does not, for android and this only works in the foreground for android: {
"to" : "fXJ_A95kTQOqm6koxOFBwh:APA91bEAUKYfn4dUhxa00yKNzs4F0b3nAh8oxXGC3cegOxhI2kCQN0XbZ0elI9M640W4SYllNF_rHQOWm8V1ryaueUN6h9ddfX2n9xfQplAzzRzBxVqDdyLa0Yl7NjigsZtlDqTeVS1i",
"data" : {
"initiatorId" : "127167807"
},
"priority": "high"
} |
Hi @stephanoparaskeva |
Hi @Dallas62 <meta-data android:name="com.dieam.reactnativepushnotification.notification_foreground"
android:value="false"/>
<service
android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationRegistrationService" />
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationActions" />
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
<meta-data
android:name="com.dieam.reactnativepushnotification.notification_channel_name"
android:value="QB_PUSH_NOTIFICATION_CHANNEL" />
<meta-data
android:name="com.dieam.reactnativepushnotification.notification_channel_description"
android:value="Channel for Quickblox push notification events" />
<meta-data
android:name="com.dieam.reactnativepushnotification.notification_color"
android:resource="@color/primary" />
<service android:name="com.quickblox.messages.services.fcm.QBFcmPushInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service> And here is the channel creation in index.js PushNotification.configure({
onRegister,
onNotification,
popInitialNotification: true,
requestPermissions: true,
});
PushNotification.createChannel(
{
channelId: 'QB_PUSH_NOTIFICATION_CHANNEL', // (required)
channelName: 'QB_PUSH_NOTIFICATION_CHANNEL', // (required)
playSound: false, // (optional) default: true
soundName: 'default', // (optional) See `soundName` parameter of `localNotification` function
},
created => console.log(`createChannel returned '${created}'`) // (optional) callback returns whether the channel was created, false means it already existed.
); I am sending the notification via Postman and it triggers I am sending a data message via postman and here is the JSON: {
"to" : "fXJ_A95kTQOqm6koxOFBwh:APA91bEAUKYfn4dUhxa00yKNzs4F0b3nAh8oxXGC3cegOxhI2kCQN0XbZ0elI9M640W4SYllNF_rHQOWm8V1ryaueUN6h9ddfX2n9xfQplAzzRzBxVqDdyLa0Yl7NjigsZtlDqTeVS1i",
"data" : {
"initiatorId" : "127167807"
},
"priority": "high"
} This works in the foreground but not in the background Regards |
@stephanoparaskeva <meta-data
android:name="com.dieam.reactnativepushnotification.default_notification_channel_id"
android:value="QB_PUSH_NOTIFICATION_CHANNEL" /> If the channel is not created and used, the notification will not work. |
Hi @Dallas62
I have tried this too and it doesnt work. I have also removed this channel entirely and it doesnt work. {
"to" : "fXJ_A95kTQOqm6koxOFBwh:APA91bEAUKYfn4dUhxa00yKNzs4F0b3nAh8oxXGC3cegOxhI2kCQN0XbZ0elI9M640W4SYllNF_rHQOWm8V1ryaueUN6h9ddfX2n9xfQplAzzRzBxVqDdyLa0Yl7NjigsZtlDqTeVS1i",
"data" : {
"initiatorId" : "127167807"
},
"priority": "high"
} It works in the foreground but does not work in the background. There must some other reason, the request is made via postman and is a data request, it does not specify a The data notification is sent via postman but the application never runs the On IOS I can successfully get the What have I done wrong on the android side? Here are some screenshots of the postman request: And here is a screenshot of the |
You must setup and use a channel (created, set as default of specified in notification payload). https://developer.android.com/training/notify-user/channels
Firebase documentation (Android 2b.):
Then, we will be able to investigate. If everything is set up, you can check the log of the device with logcat. |
Hi @Dallas62 I am using "react-native-push-notification": "^7.4.0" |
how can i create push notification without using firebase and onesignal? |
without firebase you can not create remote push notification if you want to create local push notifications then you can create it with react native push notification module |
Bug
When I am calling fcm server push notification api data is not showing on onNotification function.
Environment info
react-native info
output:Library version: 3.4.0
Steps To Reproduce
I use the following code for recieving push notification
...
Describe what you expected to happen:
Reproducible sample code
PushNotification.configure({
// (optional) Called when Token is generated (iOS and Android)
onRegister: function (token) {
console.log("TOKEN:", token);
thats.setState({deviceToken:token});
},
The text was updated successfully, but these errors were encountered: