0% found this document useful (0 votes)
10 views

Mad Assignment 7

Uploaded by

swkqmfzf8r
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Mad Assignment 7

Uploaded by

swkqmfzf8r
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

MAD ASSIGNMENT 07

Name: harshad Gurudas jogdande

Roll No:08

DIV:N

Prn: 12310341

Implement an application that creates an alert upon receiving a


message.

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:text="Notify"

android:textColor="@color/black"

android:textColorHighlight="#2196F3"

android:textColorHint="#9C27B0"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent"

app:layout_constraintVertical_bias="0.225" />

</androidx.constraintlayout.widget.ConstraintLayout>

JAVA :
package package com.example.notification;

import android.app.NotificationChannel;

import android.app.NotificationManager;

import android.content.pm.PackageManager;

import android.os.Build;

import android.os.Bundle;

//import android.support.v4.app.NotificationCompat;

//import android.support.v4.app.NotificationManagerCompat;

//import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import androidx.core.app.ActivityCompat;

import androidx.core.app.NotificationCompat;

import androidx.core.app.NotificationManagerCompat;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button b1;

b1 = (Button) findViewById(R.id.button);
createnotification();

b1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

NotificationCompat.Builder builder = new

NotificationCompat.Builder(MainActivity.this, "MyNotification");

builder.setContentTitle("Hello there! You have successfully

executed the assignment.");

builder.setContentText("Hello there! You have successfully

executed the assignment.");

builder.setSmallIcon(R.drawable.ic_launcher_foreground);

NotificationManagerCompat m =

NotificationManagerCompat.from(MainActivity.this);

if (ActivityCompat.checkSelfPermission(MainActivity.this,

android.Manifest.permission.POST_NOTIFICATIONS) !=

PackageManager.PERMISSION_GRANTED) {

// TODO: Consider calling

// ActivityCompat#requestPermissions

// here to request the missing permissions, and then

overriding

// public void onRequestPermissionsResult(int

requestCode, String[] permissions,

// int[]

grantResults)

// to handle the case where the user grants the

permission. See the documentation

// for ActivityCompat#requestPermissions for more details.

return;

}
m.notify(0, builder.build());

});

private void createnotification() {

CharSequence name = "MAD Lab";

String description = "Assignment10";

int importance = NotificationManager.IMPORTANCE_DEFAULT;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)

NotificationChannel ch = new

NotificationChannel("MyNotification",name,importance);

ch.setDescription(description);

NotificationManager n =

getSystemService(NotificationManager.class);

n.createNotificationChannel(ch);

}}}

OUTPUT

You might also like