how to create simple notification in android studio which get text from edit text or your own text . on button click notification show .when user click on this notification any activity open

Notification are most common used thing in any app today we are going to show an notification which show title and text content in notification . also open activity on click notification 



step :1 create a App class and create an notification channels

public class App extends Application {

    public static final String CHANNEL_ID = "channel1";

    @Override
    public void onCreate() {
        super.onCreate();
        creatnotificationchannel();

    }

    private void creatnotificationchannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "personal notification";
            String description = "personal informaton all are here";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;

            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, name, importance);
            notificationChannel.setDescription(description);

            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(notificationChannel);
        }
    }

}

step :2 add app class into menifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.notifiaction">

    <application
        android:name=".App" // This is app class 
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        </application>

</manifest>

step 3: get text from edit text or set your own text into notification call this method on button click

 public void btn_simplenotification(View view) {

        // if you are taking text from user inout then use both below lines
       // String stile = txttitle.getText().toString().trim();
       // String smessage = txtmessage.getText().toString().trim();

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
        builder.setContentTitle("this is title of message ");
        builder.setSmallIcon(R.drawable.ic_notifications_active_black_24dp);
        builder.setContentText("This is content text of notification");
        builder.setPriority(NotificationCompat.PRIORITY_HIGH);


        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
        notificationManagerCompat.notify(1, builder.build());
}

step 4:if you want to open any activity on notification click then use pending intent and pass activity which you want to open

 public void btn_onlciknotification(View view) {

   
      // if you are taking text from user inout then use both below lines
       // String stile = txttitle.getText().toString().trim();
       // String smessage = txtmessage.getText().toString().trim();

        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
        builder.setContentTitle("this is title of message ");
        builder.setSmallIcon(R.drawable.ic_notifications_active_black_24dp);
        builder.setContentText("This is content text of notification");
        builder.setContentIntent(pendingIntent);
        builder.setPriority(NotificationCompat.PRIORITY_HIGH);


        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
        notificationManagerCompat.notify(1, builder.build());

    }

Thank You Keep Learning Keep Connected #AndroidShortCode

Comments