Local Notification Using Java
Local Notification is not like push notification when information is received from the server side then we use push notification.
Push Notification is not part of your Current app, we have to use Android Notification Chanales we should request to the android through NotificationManger Class then we can send messages to the Android panel to show on Notification.
public class MainActivity extends AppCompatActivity {
public static final String NOTIFICATION_NAME="Example";
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Local Notification is not like push notification when information is received from the server side then we use push notification.
Push Notification is not part of your Current app, we have to use Android Notification Chanales we should request to the android through NotificationManger Class then we can send messages to the Android panel to show on Notification.
Steps To Show Notification:
XML file:<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:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLay
Java Code:
public class MainActivity extends AppCompatActivity {
public static final String NOTIFICATION_ID="1";public static final String NOTIFICATION_NAME="Example";
NotificationManager notificationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
binding.button.setOnClickListener(v->{
NotificationCompat.Builder n= new NotificationCompat.Builder(getApplicationContext(),NOTIFICATION_ID);n.setContentTitle("My Notification");
n.setContentText("All Things Worked");n.setSmallIcon(R.drawable.ic_android_black_24dp);
notificationManager.notify(1,n.build());
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
getNotification();}
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void getNotification() {
NotificationChannel notificationChannel= new NotificationChannel(NOTIFICATION_ID,NOTIFICATION_NAME,NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
}


Comments
Post a Comment