Как создать уведомление - PullRequest
0 голосов
/ 26 мая 2018

Я хочу создать простое уведомление в своем приложении, но не могу.Я искал это, я читал вопросы здесь и Google Doc, но я не знаю, почему это не работает.Я посмотрел этот код из видео, но он тоже не работает.

private final String CHANNEL_ID = "personal_notifications";
private final int NOTIFICATION_ID = 001;

public void displayNotification(View view) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this,CHANNEL_ID);
        builder.setSmallIcon(R.drawable.power);
        builder.setContentTitle("Noti");
        builder.setContentText("mukodj mar");
        builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);

        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);

Ответы [ 2 ]

0 голосов
/ 26 мая 2018

Я понял.Я сделал канал уведомлений, и теперь он работает, в любом случае, спасибо, ребята!

0 голосов
/ 26 мая 2018

Я использую этот код в BroadcastReceiver:

public class NotificationReceiver extends BroadcastReceiver{


@Override
public void onReceive(Context context, Intent intent) {

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Intent repeating_intent = new Intent(context, MainActivity.class);
    repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, repeating_intent, PendingIntent.FLAG_UPDATE_CURRENT);

StringBuilder sb = new StringBuilder();
NotificationCompat.BigTextStyle contentStyle = new NotificationCompat.BigTextStyle();
        contentStyle.bigText((CharSequence) sb.toString()); 
NotificationCompat.Builder builder = new NotificationCompat.Builder(context) // channel ID missing
                    .setContentIntent(pendingIntent)
                    .setSmallIcon(R.drawable.icon)
                    .setStyle(contentStyle)
                    .setContentTitle("Title")
                    .setContentText(sb.toString())
                    .setAutoCancel(true);
            notificationManager.notify(100, builder.build());
}

Я думаю, вы забыли последнюю строку:

notificationManager.notify(100, builder.build());
...