Personal record Notification

Related knowledge

NotificationCompat.Builder upgrade to Android O version creation method

setContentTitle set title

setContentText setting content

setSubText set small words below content

setWhen set notification time

setLargIcon set large icon

Setsmalleicon sets the small iconlarge iconlarge iconlarge iconlarge iconlarge iconlarge iconlarge iconlarge iconlarge iconlarge iconlarge iconlarge iconlarge

Setautoconcel whether or not the user clicks to notify disappears does not disappear by default

PendingIntent delay, pending Intent, used to click Notification to jump to related pages

Static methods for the PedingIntent object:

  • getActivity jumps to an Activity component
  • getBroadcast open broadcast component
  • getService open service component
public class MainActivity extends AppCompatActivity {

    private AppCompatButton btn_notify;
    private Notification notification;

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

        final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //Set Click to jump to the Intent of related activities
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
        
        notification = new NotificationCompat.Builder(this, "default")
                .setContentTitle("This is title")
                .setContentText("This is content text")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round))
                .setSubText("Small text")
                .setAutoCancel(true)
                .setContentIntent(pi)
                .build();

        btn_notify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (manager != null) {
                    manager.notify(1, notification);
                }
            }
        });
    }
}

 

Keywords: Android

Added by Baseball on Sun, 05 Jan 2020 15:18:10 +0200