React-native-push-notification: I can't see background notification data

Created on 22 Jul 2020  路  23Comments  路  Source: zo0r/react-native-push-notification

Hello,
I initiate a manual notification in the background with a request from the server. I can see the data when I receive the notification when the application is open, but when the application is closed, there is no data when I click on the notification

I did it by double checking the settings again. the notification receive is OK, but I cannot get the data.

what i want: whenI click on the notification, the application should open and open a page according to the data. (not if there is data or not, according to the user id. There is always data in it)

Most helpful comment

Solved the problem and to explain;
Splash Screen library prevents sending data to a MainActivity
When editing SplashActivity file as below, the problem is solved and popInitialNotification is working

protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       Intent intent = new Intent(this, MainActivity.class);
       intent.putExtras(getIntent().getExtras()); //added this line
       startActivity(intent);
       finish();
}

All 23 comments

Hi @yusufsancakk
Can you provide information ?
You are talking about notification/data and probably logs but there is nothing in the issue.
How can we help you with out:

  • Notification payload
  • .configure()
  • and probably the code that should handle redirection ?

I send a notification through my server and it contains data. so far it's not about your library. If it's been 1 hour, I create a notification with your library in the background;

PushNotification.localNotification({ smallIcon: 'ic_noti_icon', title: remoteMessage.data.title, message: "Haberin detay谋n谋 okumak i莽in dokunun", });

I get a notification, but when I click on the notification, I can't see the data. Trying to capture actions like this in ComponentDitMount on App.js page:

`async componentDidMount(){

PushNotification.configure({

  onRegister: function (token) {
    console.log("TOKEN:", token);
  },

  onNotification: function (notification) {
    console.log("NOTIFICATION:", notification);
  },

  onAction: function (notification) {
    console.log("ACTION:", notification.action);
    console.log("NOTIFICATION:", notification);
  },

  onRegistrationError: function(err) {
    console.error(err.message, err);
  },

  permissions: {
    alert: true,
    badge: true,
    sound: true,
  },

  popInitialNotification: true,

  requestPermissions: true,

});`

If the application is open;
onNotification () works but Action () doesn't work when I click

If the application is closed;
nothing happens when i click all logs are empty

hi @yusufsancakk
The problem is that you are trying to catch background task in a component.
You must put .configure() and any logic that run in background outside of a component.

onAction is called only when the application is in background and invokeApp is false and also there is actions set, it's not the case in your example.

how can i solve this? When I send a data in the notification and start the application, how do I show that data?

If you need to forward data in the localNotification, you can use:

PushNotification.localNotification({ smallIcon: 'ic_noti_icon', title: remoteMessage.data.title, message: "Haberin detay谋n谋 okumak i莽in dokunun",  data: remoteMessage.data  });

I check the incoming data and send it, but I cannot receive the data. I tried invokeApp. Only the token getToken is working. I can't get actions and data
[Wed Jul 22 2020 14:49:53.157] LOG TOKEN: {"os": "android", "token": "dgNzLtwCTiSj15-zR1d7Ew:APA91bEFfKDJ8SoK5sE_61s0KoiVCVPGWdYJgD1Jcglw8RevNcpuYcQbT3KVcWG6S63UHq6rxptr6cExXcRzjj0FQLODjGPFYjrxrA0MOfqJEonUX"}

Did you put the .configure() outside of the component App ?

.configure() in componentDidMount ()

Don't do that, put it outside of ANY component

Yeah! When I wrote other than App.js, I was able to see the data when the background notification came. But I want to capture the data when I click. Not when the notification arrives

You need to use PushNotification.getInitialNotification(callback) to get the notification that open the Application

Yes. I get such an error when I use it

Possible Unhandled Promise Rejection (id: 4): TypeError: _reactNativePushNotification.default.getInitialNotification is not a function. (In '_reactNativePushNotification.default.getInitialNotification()', '_reactNativePushNotification.default.getInitialNotification' is undefined) componentDidMount$

Sorry, on this library it's popInitialNotification, this was the iOS library method.

so there is no solution for android?

I ran the popInitialNotification function for android but i see 'null' in log

There is probably an activity that intercept the notification, look in issues, this case has been already reported and solved.

@yusufsancakk can you try hitting the below API from the postman with the required headers & body. Just wanted to check if you are passing the payload correctly for Android.

To see how the custom data is nested for different app states like foreground/killed you can refer the below post
https://medium.com/enappd/firebase-push-notifications-in-react-native-apps-d9f60726ce9c

FCM API
https://fcm.googleapis.com/fcm/send

HEADERS
Authorization: key=_YOUR_SERVER_KEY_,
Content-Type: application/json

BODY
{
"to" : "_YOUR_DEVICE_TOKEN_",
"notification" : {
"body": "Body of Your Notification",
"title": "Title of Your Notification"
},
"data" : {
"campaign_id" : 45,
"body": "Body of Your Notification",
"title": "Title of Your Notification"
}
}

@Dallas62 I couldn't find the solved issue. Can you send the link? and it doesn't even work on the first line in a new project

Solved the problem and to explain;
Splash Screen library prevents sending data to a MainActivity
When editing SplashActivity file as below, the problem is solved and popInitialNotification is working

protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       Intent intent = new Intent(this, MainActivity.class);
       intent.putExtras(getIntent().getExtras()); //added this line
       startActivity(intent);
       finish();
}

@yusufsancakk thanks for the suggested solution. Can you advice regarding SplashActivity.java file ? I don't see this file neither on the native android files of react-native-splash-screen nor on the android RN project itself. Did you create this or ... ? I need just some basic details to get this up and running, thanks :)

@yusufsancakk you saved my day!! I was looking for this solution for a couple of days!

@marudy Yes. I created the SplashScreen.java file. You can do this by reading this article: https://medium.com/@appstud/add-a-splash-screen-to-a-react-native-app-810492e773f9

Was this page helpful?
0 / 5 - 0 ratings