Skip to content
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

I can't see background notification data #1550

Closed
ysancak opened this issue Jul 22, 2020 · 23 comments
Closed

I can't see background notification data #1550

ysancak opened this issue Jul 22, 2020 · 23 comments

Comments

@ysancak
Copy link

ysancak commented Jul 22, 2020

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)

@Dallas62
Copy link
Collaborator

Dallas62 commented Jul 22, 2020

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 ?

@ysancak
Copy link
Author

ysancak commented Jul 22, 2020

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

@Dallas62
Copy link
Collaborator

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.

@ysancak
Copy link
Author

ysancak commented Jul 22, 2020

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

@Dallas62
Copy link
Collaborator

Dallas62 commented Jul 22, 2020

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  });

@ysancak
Copy link
Author

ysancak commented Jul 22, 2020

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"}

@Dallas62
Copy link
Collaborator

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

@ysancak
Copy link
Author

ysancak commented Jul 22, 2020

.configure() in componentDidMount ()

@Dallas62
Copy link
Collaborator

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

@ysancak
Copy link
Author

ysancak commented Jul 22, 2020

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

@Dallas62
Copy link
Collaborator

Dallas62 commented Jul 22, 2020

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

@ysancak
Copy link
Author

ysancak commented Jul 22, 2020

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$

@Dallas62
Copy link
Collaborator

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

@ysancak
Copy link
Author

ysancak commented Jul 22, 2020

so there is no solution for android?

@ysancak
Copy link
Author

ysancak commented Jul 22, 2020

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

@Dallas62
Copy link
Collaborator

Dallas62 commented Jul 22, 2020

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

@KPS250
Copy link

KPS250 commented Jul 22, 2020

@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"
}
}

@ysancak
Copy link
Author

ysancak commented Jul 22, 2020

@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

@Dallas62
Copy link
Collaborator

Check this one:
#1378 (comment)

@ysancak
Copy link
Author

ysancak commented Jul 22, 2020

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();
}

@marudy
Copy link

marudy commented Aug 23, 2020

@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 :)

@matanrokach
Copy link

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

@ysancak
Copy link
Author

ysancak commented Oct 8, 2020

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants