Search code examples
phpandroidnotificationsgoogle-cloud-messagingpayload

Android GCM notification : no title


I'am building an Android app and currently working on notifications. I'm using the Google Cloud Messaging service, for which I wrote the following PHP code :

<?php
define('API_ACCESS_KEY', 'blablabla');
$registrationIds = array($this->deviceId);

$data = array
(
    'title' => 'My Title',
    'message' => 'My message',
    'subtitle' => 'This is a subtitle. subtitle',
    'tickerText' => 'Ticker text here...Ticker text here...',
    'vibrate' => 1,
    'sound' => 1
);

$fields = array
(
    'registration_ids' => $registrationIds,
    'data' => $data
);

$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));
curl_exec($ch);
curl_close($ch);

When i execute this php file, I receive the notification on my Android device, as you can see on the following screenshot. The message text is ok. android screenshot But the problem is that the notification's title is not 'My Title' as I wrote in the payload, but 'GCM Message' (looks like default GCM title). Tried a lot of different things but didn't figured it out.

Any ideas where I'm wrong ? Thanks

-------- SOLUTION --------

I had totaly forgot this has to be handled on the android side... The solution was to custom the sendNotification() method in MyGcmListenerService to handle extra data :

private void sendNotification(String title, String message) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

Thanks for answers !


Solution

  • in order to set a custom notification title you should create your own notification inside onMessageReceived(String form, Bundle data) method of GcmListenerService class...what i did to create a custom notification is described in the following code:

    public class MyGcmListenerService extends GcmListenerService {
    
    private PendingIntent resultPendingIntent;
    
    @Override
    public void onMessageReceived(String from, Bundle data) {
        NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.app_logo)
                    .setContentTitle(data.getString(getResources().getString(R.string.notification_title)))
                    .setContentText(data.getString(getResources().getString(R.string.notification_body)))
                    .setDefaults(Notification.DEFAULT_VIBRATE)
                    .setContentIntent(resultPendingIntent);
    
            int mNotificationId = 001;
            NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            mNotifyMgr.notify(mNotificationId, nBuilder.build());
      }
     }
    

    where in setContentTitle you set your custom notification title.