Notification
Notification
If you give the foreign application an Intent, it will execute your Intent with its own permissions.
But if you give the foreign application a PendingIntent, that application will execute
your Intent using your application's permission.
Note: Why PendingIntent is required Why the receiving application itself cannot create
the Intent or
The limitation is overcome using PendingIntent. With PendingIntent the receiving application,
doesn't need to have android.permission.BLUETOOTH_ADMIN for enabling Bluetooth.
// notificationId is a unique int for each notification that you must define
notificationManager.notify(notificationId, builder.build());
Context
Int32
Intent
PendingIntentFlags
Returns
PendingIntent
Returns an existing or new PendingIntent matching the given parameters. May return null
only if #FLAG_NO_CREATE has been supplied.
1. Construct a NotificationChannel object with a unique channel ID, a user-visible name, and an
importance level.
2. Optionally, specify the description that the user sees in the system settings
with setDescription().
Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="328dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.notification;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationManagerCompat;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
getSystemService(NotificationManager.class).createNotificationChannel(chann
el);
Notification.Builder notification = new
Notification.Builder(MainActivity.this, CHANNEL_ID)
.setContentTitle("title")
.setContentText("body")
.setSmallIcon(R.drawable.ic_launcher_background)
.setAutoCancel(true);
NotificationManagerCompat.from(MainActivity.this).notify(1,
notification.build());
}
});
}
}