57

如何制作长通知多行。我正在使用下面的代码片段,但它不起作用:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
  .setContentTitle(title)
  .setSmallIcon(R.drawable.icon)
  .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
  .setContentText(message)
  .setContentIntent(pIntent);

return mBuilder.build();
4

5 回答 5

125

添加这个:

NotificationCompat.Builder builder = new NotificationCompat.Builder(
            context);
    Notification notification = builder.setContentIntent(contentIntent)
            .setSmallIcon(icon).setTicker(appname).setWhen(0)
            .setAutoCancel(true).setContentTitle(appname)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setContentText(message).build();

    notificationManager.notify(0, notification);

另请检查:多行通知

于 2013-11-06T08:20:56.947 回答
12
Bitmap icon1 = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);  
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(getNotificationIcon()).setLargeIcon(icon1)
            .setTicker(title)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setTicker(message)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(title))
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setPriority(Notification.PRIORITY_MAX)
            .setContentIntent(pIntent)
            .setContent(remoteViews)
             .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setVibrate(new long[] {1, 1, 1})
             .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_launcher))
            .setDefaults(Notification.DEFAULT_SOUND)
            .setDefaults(Notification.DEFAULT_ALL);
    NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification notify = new Notification();
    notify.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_ONLY_ALERT_ONCE;
    Random random = new Random();
    notificationmanager.notify(random.nextInt(), builder.build());
于 2016-01-18T13:33:45.997 回答
5

试试这个代码与 android Oreo 通知通道 id 一起正常工作。

public static int id = 0;
private void sendNotification(String messageBody) {

    Log.d("FCMID","id : " + id);
    Intent intent = new Intent(this, Main2Activity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent,PendingIntent.FLAG_ONE_SHOT);

    String channelId = "1";
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.drawable.ic_notification)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
            .setContentTitle("Piml Sid")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId,
                "Spike Bot",
                NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }
    id ++;
   notificationManager.notify(id /* ID of notification */, notificationBuilder.build());
}
于 2018-04-12T09:50:57.680 回答
1

在你NotificationCompat.Builder添加

.setStyle(NotificationCompat.BigTextStyle().bigText(body))
.setContentText(body) 

.setContentText(body)

于 2020-05-06T06:48:29.423 回答
-2
Notification notification = new Notification.Builder(context)
         .setContentTitle(title)  
         .setSmallIcon(icon) 
         .setStyle(new Notification.BigTextStyle().bigText(notificationMessage))
         .setAutoCancel(true)
         .setContentIntent(intent)
         .build();
于 2015-02-11T11:02:21.320 回答