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

Data Message can't receive #39

Closed
Sakura777 opened this issue Jan 12, 2017 · 5 comments
Closed

Data Message can't receive #39

Sakura777 opened this issue Jan 12, 2017 · 5 comments

Comments

@Sakura777
Copy link

Sakura777 commented Jan 12, 2017

Unity 5.4.4f1
SO-02E Android 4.4.2

I have built quickstart project and can receive Notification correctly.
But Data Messages sent from PHP can't receive.

<?php

$api_key  = "MY_KEY";
$base_url = "https://fcm.googleapis.com/fcm/send";


//Data Message : It can't receive
$data = array(
    "to"           => "DEVICE_ID",
    "priority"     => "high",
    "data"         => array(
        "message"   => "Message"
    )
);

/*
//notification : It can receive
$data = array(
     "to"           => "DEVICE_ID",
     "priority"     => "high",
     "notification" => array(
        "title" => "title",
        "body"  => "body"
    )
);
*/
$header = array(
     "Content-Type:application/json",
     "Authorization:key=".$api_key
);
$context = stream_context_create(array(
    "http" => array(
         'method' => 'POST',
         'header' => implode("\r\n",$header),
         'content'=> json_encode($data)
    )
));

echo file_get_contents($base_url,false,$context);

Same message is returned for both Data Message and Notification in echo.
(Even Data Message returns "success":1 )
{"multicast_id":~~~,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"~~~"}]}

But on device,
Notification : Messages appear in the notification list.
Data Message : Messages are not displayed anywhere.

By the way, when sending by inputting detailed options from Firebase Console,
messages appear in the notification list.

I think there is a problem with the data setting method, but I do not know where it is.
Could you just tell us what you think is the cause?

@stewartmiles
Copy link
Contributor

The details of the expected behavior are here
https://firebase.google.com/docs/cloud-messaging/android/receive:

  • When the app is running in the foreground the notification is routed
    from the service (that handles the notification) to the SDK which then
    fires the callback.
  • When the app is in the background and running we can route the
    notification to the app, as it's running :)
  • When the app isn't running / killed, our service runs which receives
    the notification and caches it. When the user clicks on the notification
    the notification is routed to the app when it's launched.

If you want to customize what happens in the notification when the app
isn't running you'll need to subclass our service
(com.google.firebase.messaging.cpp.ListenerService which implements
FirebaseMessagingServices
https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessagingService)
and replace the reference to it in your Android manifest. You can then
override the onMessageReceived() method to customize the behavior in your
service. Obviously, this is not particularly cross platform as there is no
way to do this on iOS.

Does this clarify the behavior you're seeing? Are you able to reproduce the errant behavior with our sample application?

Cheers,
Stewart

@stewartmiles
Copy link
Contributor

I guess there is no more feedback since my last reply, I hope it's working for you. Please reopen if you still have issues.

@muneebattamindravel
Copy link

I am having the same issue.
I am not getting the Data Messages. Here is the complete detail of what i am doing and what is happening:

  • I am using Unity 5.6.1f1 and latest Firebase SDK for Messaging.
  • I am using a php script to send the messages.
  • The notification messages are displayed fine. Custom data fields in the "Notification Message" (in t the "data" key) are also retrieved fine in the "OnMessageReceived" method, with the following script:
    `<?php
    $json_notificationMessage = '{ "data": {
    "command": "hit",
    "challengeScore": "35"
    },
    "notification":{
    "title":"Hello Title",
    "body":"Hello Body",
    "sound":"default"
    }
    "to": "DEVICE_TOKEN",
    "priority": "high"
    }';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Authorization:key=SERVER_KEY'
    ));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json_notificationMessage);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);
    echo $output;

But the PROBLEM WITH THIS is that i have to click on the notification to get the OnMessageReceived function called.

  • Now i am sending the FCM Data Message so that i don't have to click on the notification to get the OnMessageReceived function called. But i am not receiving the Data Messages when my app is in background or closed (quit). Following is the php script that i am using for sending the Data Message:

$json_dataMessage = '{ "data": {
"command": "hit",
"challengeScore": "35"
},
"to": "DEVICE_TOKEN",
"priority": "high"
}';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization:key=SERVER_KEY'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_dataMessage);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$output = curl_exec($ch);
curl_close($ch);
echo $output;`

  • I read the following note from the "About FCM Messages" page of Firebase documentation

"Note: If you want to send messages consisting of only custom key-values to an iOS device when the app is in the background, set custom key-value pairs in the data key and set content_available to true."

so i added the content_available:true so that my jSON now looks like this:

$json_dataMessage = '{ "data": {
"command": "hit",
"challengeScore": "35"
},
"to": "DEVICE_TOKEN",
"priority": "high",
"content_available":true
}';

And i received the Data Message BUT ONLY when my app was in foreground (visible on screen).

  • My requirement is that i want to get the Data Payload even if a user does not clicks on the notification. According to FCM, Data Message is the way to do this but i am not getting that custom data payload if the app is in background or closed.

Please help in this regard on how can i achieve this functionality.

Thank-you.

@stewartmiles
Copy link
Contributor

@muneebattamindravel the behavior of data messages differs between Android and iOS

For Android details see
https://firebase.google.com/docs/cloud-messaging/android/receive
When messages are received in the background, if the application's message receive service is running it's possible to perform an action in response to a message. We have this documented in the C guide (the Unity SDK is built on top of the C SDK) https://firebase.google.com/docs/cloud-messaging/cpp/client#custom_message_handling_on_android

On iOS when an app is in the background (it's really not running) messages are received via APNS in which case data will only be received by the application when / if the message is clicked and the message is routed to the app when it starts running again.

@muneebattamindravel
Copy link

muneebattamindravel commented Aug 25, 2017 via email

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

No branches or pull requests

3 participants