0% found this document useful (0 votes)
32 views6 pages

Notification

Uploaded by

SARTHAK GARAI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views6 pages

Notification

Uploaded by

SARTHAK GARAI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Notification is a kind of message, alert, or status of an application (probably

running in the background) that is visible or available in the Android’s UI elements.


This application could be running in the background but not in use by the user.
Create Notification Builder
Notification nt=new Notification.Builder(MainActivity.this, CHANNEL_ID)
The properties of Android notification are set using NotificationCompat.Builder object.
Note: Notice that the NotificationCompat.Builder constructor requires that you provide a channel ID.
This is required for compatibility with Android 8.0 (API level 26) and higher, but is ignored by older
versions.
Setting Notification Properties
Notification properties:
o setSmallIcon(): It sets the icon of notification.
o setContentTitle(): It is used to set the title of notification.
o setContentText(): It is used to set the text message.
o setAutoCancel(): It sets the cancelable property of notification.
o setPriority(): It sets the priority of notification.

PendingIntent is a tokenthat you give to a foreign application


(e.g. NotificationManager, AlarmManager, Home Screen AppWidgetManager, or other 3rd party
applications), which allows the foreign application to use your application's permissions to
execute a predefined piece of code.

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

1. Why we cannot use a simple Intent for the same purpose.


E.g.Intent bluetoothIntent= new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

If I send bluetoothIntent to another application, which doesn't have


permission android.permission.BLUETOOTH_ADMIN, that receiving application cannot enable
Bluetooth with startActivity(bluetoothIntent).

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());

GetActivity(Context, Int32, Retrieve a PendingIntent that will start a new activity,


Intent, PendingIntentFlags)
Parameters
context

Context

The Context in which this PendingIntent should start the activity.


requestCode

Int32

Private request code for the sender


intent

Intent

Intent of the activity to be launched.


flags

PendingIntentFlags

May be #FLAG_ONE_SHOT, #FLAG_NO_CREATE, #FLAG_CANCEL_CURRENT, #FLAG_UPDATE_CURRENT, or


any of the flags as supported by Intent#fillIn Intent.fillIn() to control which unspecified parts of
the intent that can be supplied when the actual send happens.

Returns
PendingIntent

Returns an existing or new PendingIntent matching the given parameters. May return null
only if #FLAG_NO_CREATE has been supplied.

Channel Id is a unique String to identify each NotificationChannel and is used in Notification.


Builder (line 7) when constructing the Notification object.

Heads-up notification appears the moment your app issues the


notification and it disappears after a moment, but remains visible in the
notification drawer as usual.
All notifications must be assigned to a channel. For each channel, you can set the visual and auditory
behavior that is applied to all notifications in that channel. Then, users can change these settings and
decide which notification channels from your app should be intrusive or visible at all.
Create a notification channel
To create a notification channel, follow these steps:

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().

3. Register the notification channel by passing it to createNotificationChannel().

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;

public class MainActivity extends AppCompatActivity {


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

private void notifyMe() {


btn=(Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent();
PendingIntent pIntent =
PendingIntent.getActivity(MainActivity.this,0,intent,PendingIntent.FLAG_IMM
UTABLE);

final String CHANNEL_ID = "HEADS_UP_NOTIFICATIONS";


NotificationChannel channel = new NotificationChannel(
CHANNEL_ID,
"MyNotification",
NotificationManager.IMPORTANCE_HIGH);

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());

}
});
}
}

You might also like