2

This is my code to send Notification :

$msg = array
(
    "title" => "Portugal vs. Denmark",
    "message" => "5 to 1"
);

$fields = array (
    "to" => $deviceToken,
    "data" =>  $msg
);

$headers = array
(
    'Authorization: key=' . API_ACCESS_KEY,
    'Content-Type: application/json'
);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );

I receive the "5 to 1" message but the title "Portugal vs. Denmark" is not displayed.

Does anyone have an idea ?

In the data field I tried alert, subtitle and they did not change anything.

Thank's

EDIT

Screenshot of the notification I have

angelcool.net
  • 2,505
  • 1
  • 24
  • 26
nocalis
  • 156
  • 2
  • 13
  • Take a look at https://gist.github.com/prime31/5675017 and play with his bundle array. Not that I notice anything immediately wrong with yours, but it may help. – zgc7009 May 12 '16 at 15:33
  • I just saw the link zgc7009 sent, and comparing it with yours, it seems ok. How are you checking the received fields ? maybe you're problem could lie there... just brainstorming ;) – Sergio Lima May 12 '16 at 15:40
  • I tried these fields and this is still the same :/ I will post a screenshot of what i got. – nocalis May 12 '16 at 15:54
  • the problem could be on the code you wrote to display the notification. you might be calling the NotificationCompat.Builder wrong. Can you post that piece of code ? – Sergio Lima May 12 '16 at 16:54
  • Not sure why yours isn't working, but for what it's worth I usually make a 1D array when I pass to GCM and it works fine. – Zach Rattner May 13 '16 at 00:42

2 Answers2

0

Try sending the below json as the paylod. If it works, the problem might be on the array() function. It should creat a dictionary, not an array i believe

{ 
   "data": {
   "score": "5x1",
   "title" : "portugal vs Denmark",
   "message":"5 to 1"
   },
   "to": "YOUR DEVICE TOKEN",
}

EDIT:

Have a look into this pseudo code, and compare with yours. You have to get the fields from the bundle data that comes on the push notification.

@Override
    public void onMessageReceived(String from, Bundle data) {
        String message = data.getString("message");
        String title = data.getString("title");
        String score = data.getString("score");

///...

NotificationCompat.Builder notificationBuilder = new     NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_ic_notification)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

//....
}
Sergio Lima
  • 114
  • 13
0

this is pretty far fetched but can you change 'message' to something else like 'myinfo' or something, then extract it on your client's side (making the necessary changes). I'm thinking 'message' might be interpreted according to a standard ,and the rest is ignored.

also try to display the content on your android studio just so you can see if you are actually getting the title on your client's side.

Pc hobbit
  • 96
  • 1
  • 11
  • It's build on Unity, can I use android studio anyway ? – nocalis May 17 '16 at 14:35
  • yeah ofc (making the neccery changes) . I think if you are going to use the same exact code above you can use Jaxl library (without needing android studio ) http://stackoverflow.com/questions/33944550/upstream-message-to-server-app , but im not sure how it will change your project's design (in terms of your client/server set up ) – Pc hobbit May 20 '16 at 04:13