Sophie
Sophie

Reputation: 2634

Firebase Notification contains both Title and Message

I am using this tutorial to send push notification using Firebase

How can I show Title and Message both in Notification Barenter code here (Still I am getting message as *Title* content in Notification Bar)…

As you can see in this screenshot (due to this, I am unable to deliver complete *message along with Title* to users, because in Notification Bar title has limit maximum 1 line)

I am using Advanced Rest Client (Messaging API) to deliver Notification(s) like this:

https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIza************adrTY

{ "data": {
    "image": "https://ibin.co/2t1lLdpfS06F.png",
    "message": "Firebase Push Message Using API"
    "AnotherActivity": "True"
  },
  "to" : "f25gYF3***********************HLI"
}

Code

/**
     * Create and show a simple notification containing the received FCM message.
     */

    private void sendNotification(String messageBody, Bitmap image, String TrueOrFalse) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("AnotherActivity", TrueOrFalse);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setLargeIcon(image)/*Notification icon image*/
                .setSmallIcon(R.drawable.firebase_icon)
                .setContentTitle(messageBody)
                .setStyle(new NotificationCompat.BigPictureStyle()
                        .bigPicture(image))/*Notification with Image*/
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }

Upvotes: 0

Views: 2453

Answers (1)

GuilhermeFGL
GuilhermeFGL

Reputation: 2919

Use

setContentTitle("title") to set the title and of notification

setContentText("text") to set the text.

A example from Android Developers:

NotificationCompat.Builder mBuilder =
    new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("My notification")
    .setContentText("Hello World!");

In your case you will have to change your code to:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setLargeIcon(image)
                .setSmallIcon(R.drawable.firebase_icon)
                .setStyle(new NotificationCompat.BigPictureStyle()
                        .bigPicture(image))/*Notification with Image*/
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent)
                .setContentTitle(messageTitle) // here
                .setContentText(messageBody);  // and here

I recommend that you take some time studying NotificationChannel as well, if you wanna to support the new Android O notifications

NotificationChannel example

Upvotes: 1

Related Questions