0

Hey I am just learning to Android development so I am trying to make push notifications. Book I read says to build it like that. But android studio says "Builder is deprecated" about NotificationCompat.Builder(this)

I also tried to find information on the internet, but none of it works. This is my code:

package com.example.notifications;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.TaskStackBuilder;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    public static final int NOTIFICATION_ID = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public  void onClick(View view){
        NotificationCompat.Builder mBuilder =
                (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_launcher_foreground)
                        .setContentText("bioba")
                        .setContentTitle("aboba");

        Intent resultIntent = new Intent(this, MainActivity.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(MainActivity.class);
        stackBuilder.addNextIntent(resultIntent);

        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());


    }



}

I use emulator of Android 8.1

Adam
  • 186
  • 1
  • 4
  • 20
bruh
  • 47
  • 1
  • 6

1 Answers1

0

Use public Builder(@NonNull Context context, @NonNull String channelId) instead, where channelId is the NotificationChannel where the constructed Notification will be posted.

Shane Sepac
  • 806
  • 10
  • 20