Building a Notification
The examples in this class are based on the NotificationCompat.Builder class.NotificationCompat.Builder is in the Support
Library. You should use NotificationCompat and its subclasses, particularly NotificationCompat.Builder, to provide the best notification support for a wide range
of platforms.
Create
a Notification Builder
When creating a notification, specify the UI content and
actions with a NotificationCompat.Builderobject.
At bare minimum, a Builder object must
include the following:
·
A small icon, set by setSmallIcon()
·
A title, set by setContentTitle()
·
Detail text, set by setContentText()
For example:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
Define
the Notification's Action
Although actions are optional, you should add at least one
action to your notification. An action takes users directly from the
notification to an Activityin your
application, where they can look at the event that caused the notification or
do further work. Inside a notification, the action itself is defined by a PendingIntent containing an Intent that starts an Activity in your application.
How you construct the PendingIntent depends on what type of Activity you're starting. When you start an Activity from a notification, you must preserve the user's
expected navigation experience. In the snippet below, clicking the notification
opens a new activity that effectively extends the behavior of the notification.
In this case there is no need to create an artificial back stack (see Preserving Navigation when Starting an Activity for
more information):
Intent resultIntent = new Intent(this, ResultActivity.class);
...
// Because clicking the notification opens a new ("special") activity, there's
// no need to create an artificial back stack.
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
...
// Because clicking the notification opens a new ("special") activity, there's
// no need to create an artificial back stack.
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
Set
the Notification's Click Behavior
To associate the PendingIntent created
in the previous step with a gesture, call the appropriate method of NotificationCompat.Builder. For example, to start an activity when the user clicks the
notification text in the notification drawer, add the PendingIntent by calling setContentIntent(). For example:
PendingIntent resultPendingIntent;
...
mBuilder.setContentIntent(resultPendingIntent);
...
mBuilder.setContentIntent(resultPendingIntent);
Issue
the Notification
To issue the notification:
·
Get an instance of NotificationManager.
·
Use the notify() method to issue the notification. When you call notify(), specify a notification ID. You can use this ID to update
the notification later on. This is described in more detail in Managing Notifications.
·
Call build(), which returns a Notification object containing your specifications.
For
example:
NotificationCompat.Builder mBuilder;
...
// Sets an ID for the notification
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
...
// Sets an ID for the notification
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
Sample Project:
XML:
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.nice.simplenotifications.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simple Notifications!"
android:textSize="50sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type Your Notification Here"
android:id="@+id/nTxt"
/>
<Button
android:layout_marginTop="10dp"
android:background="@color/colorPrimaryDark"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:text="Send Notification"
android:textAllCaps="false"
android:textStyle="bold"
android:textColor="#fff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn"/>
</LinearLayout
>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.nice.simplenotifications.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simple Notifications!"
android:textSize="50sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type Your Notification Here"
android:id="@+id/nTxt"
/>
<Button
android:layout_marginTop="10dp"
android:background="@color/colorPrimaryDark"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:text="Send Notification"
android:textAllCaps="false"
android:textStyle="bold"
android:textColor="#fff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn"/>
</LinearLayout
>
JAVA:
package com.example.nice.simplenotifications;
import android.app.NotificationManager;
import android.content.Context;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
EditText txt;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (EditText) findViewById(R.id.nTxt);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (txt.getText().toString().isEmpty()){
sendNotification("Please type your custom notification!");
}else {
sendNotification(txt.getText().toString());
}
}
});
}
public void sendNotification(String txt) {
//Get an instance of NotificationManager//
Uri nSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
SimpleDateFormat dt = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss", Locale.getDefault());
String dt1 = dt.format(new Date());
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(txt)
.setContentText(dt1)
.setSound(nSound)
.setDefaults(NotificationCompat.DEFAULT_VIBRATE);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(001, mBuilder.build());
}
}
import android.app.NotificationManager;
import android.content.Context;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
EditText txt;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (EditText) findViewById(R.id.nTxt);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (txt.getText().toString().isEmpty()){
sendNotification("Please type your custom notification!");
}else {
sendNotification(txt.getText().toString());
}
}
});
}
public void sendNotification(String txt) {
//Get an instance of NotificationManager//
Uri nSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
SimpleDateFormat dt = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss", Locale.getDefault());
String dt1 = dt.format(new Date());
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(txt)
.setContentText(dt1)
.setSound(nSound)
.setDefaults(NotificationCompat.DEFAULT_VIBRATE);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(001, mBuilder.build());
}
}
No comments:
Post a Comment