0

Some phones do not support big notifications. So how can I know then in runtime to make my notif big or usual size? Also i need it support control buttons. Here is my code of big notif:

RemoteViews remoteViews = new RemoteViews(getPackageName(),
            settings.getAttributeId(settings.getThemeByString(), R.attr.theme_dependent_notification));

    remoteViews.setOnClickPendingIntent(R.id.prev, ppreviousIntent);
    remoteViews.setOnClickPendingIntent(R.id.play, pplayIntent);
    remoteViews.setOnClickPendingIntent(R.id.next, pnextIntent);
    remoteViews.setOnClickPendingIntent(R.id.close, pcloseIntent);

    remoteViews.setImageViewBitmap(R.id.cover, icon);
    remoteViews.setTextViewText(R.id.name, title);
    remoteViews.setTextViewText(R.id.artist, text);
    remoteViews.setTextViewText(R.id.count, count);

    if (isPlayerActive) remoteViews.setImageViewResource(R.id.play, R.drawable.pause);
    else remoteViews.setImageViewResource(R.id.play, R.drawable.play);

    notification = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.equalizer_icon)
            .setContentTitle(title)
            .setContentText(text)
            .setTicker(getString(R.string.app_name))
            .setContentIntent(pendingIntent)
            .setPriority(Notification.PRIORITY_MAX)
            .build();

    notification.bigContentView = remoteViews;

1 Answers1

0

Try this code:

public static void showNotification(Context context) {
    int notificationId = 99;
    Intent cancelIntent = new Intent(YOUR_ACTION).putExtra(YOUR_EXTRA_NOTIFICATION_ID, notificationId);
    PendingIntent buttonPendingIntent = PendingIntent.getBroadcast(context, 123, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder notBuilder = new NotificationCompat.Builder(context)
            .setAutoCancel(false)
            .setOngoing(true)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setSmallIcon(android.R.drawable.stat_notify_sync)
            .setTicker("Ticker title")
            .setOnlyAlertOnce(true)
            .setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, YourActivity.class), PendingIntent.FLAG_UPDATE_CURRENT));

    Notification not = notBuilder
            .setContentTitle("Title")
            .setContentText("Message")
            .setProgress(100, 33, false)
            .addAction(android.R.drawable.ic_menu_close_clear_cancel, "Cancel", buttonPendingIntent)
            .setStyle(new NotificationCompat.BigTextStyle().bigText("Message"))
            .build();

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(notificationId, not);
}

It shows a big notification with one button - you can enlarge it by pulling bottom edge down.

isabsent
  • 3,683
  • 3
  • 25
  • 46
  • But what about custom layout? I need to variants of notif: for phones that can show big, and usual size for phones which can't. Notifications with control buttons on custom layout – Ivan Sablin May 26 '17 at 16:19
  • Did you try https://stackoverflow.com/questions/21237495/create-custom-big-notifications or https://stackoverflow.com/questions/18367631/change-notification-layout? – isabsent May 26 '17 at 16:37
  • You do not understand me. I know how to make custom layout.You can see my code. I don't know how to show usual size notif when big is not allow. – Ivan Sablin May 27 '17 at 07:09