InboxStyle consente di visualizzare il contenuto delle notifiche in righe tramite il metodo addLine() ,che può impostare fino a 6 linee.Come negli altri esempi andiamo ad aggiungere un oggetto InboxStyle all’oggetto NotificationCompat.Builder:
activity_main.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:gravity=”center”
android:orientation=”vertical”>
<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Inbox Style”
android:id=”@+id/btn”/>
</LinearLayout>
MainActivity.java
package com.example.notification.activities;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.example.notification.R;
import android.widget.*;
import android.view.*;
import android.app.*;
import android.os.*;
import android.content.*;
import androidx.core.app.*;
public class MainActivity extends AppCompatActivity {
Button button;
String CHANNEL_ID = “notification”;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
notification();
}
});
}
private void notification() {
NotificationManager notificationManager= (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent (MainActivity. this , MainActivity. class);
PendingIntent pendingIntent = PendingIntent.
getActivity(MainActivity. this , 0 , intent, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel= new NotificationChannel(CHANNEL_ID,
“Notification”, NotificationManager.IMPORTANCE_MAX);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.
Builder(this, CHANNEL_ID);
builder.setAutoCancel(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(“6 messages”);
method(builder);
builder.setContentIntent(pendingIntent);
notificationManager.notify(0, builder.build());
}
private void method(NotificationCompat.Builder builder)
{
builder.setStyle(new NotificationCompat.InboxStyle()
.addLine(“Message 1”)
.addLine(“Message 2”)
.addLine(“Message 3”)
.addLine(“Message 4”)
.addLine(“Message 5”)
.addLine(“Message 6”)
.setSummaryText(“+ 6”));
}
}