Created
July 22, 2021 05:53
-
-
Save baponkar/97bcda3c5718abbf64f8aedf0843564d to your computer and use it in GitHub Desktop.
Send Simple text notification in android studio project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout | |
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"> | |
<Button | |
android:id="@+id/simple" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center_vertical" | |
android:layout_marginLeft="15dp" | |
android:clickable="true" | |
android:layout_marginTop="5dp" | |
android:background="@color/black" | |
android:onClick="simple_notification" | |
android:text="Simple Notification" | |
android:textColor="@color/white" /> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.sendnotification; | |
import android.app.NotificationChannel; | |
import android.app.NotificationManager; | |
import android.media.RingtoneManager; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import androidx.appcompat.app.AppCompatActivity; | |
import androidx.core.app.NotificationCompat; | |
public class MainActivity extends AppCompatActivity { | |
Button btn; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
btn = findViewById(R.id.simple); | |
} | |
public void simple_notification(View view){ | |
int notificationId = 0; | |
NotificationCompat.Builder builder = new NotificationCompat.Builder(this); | |
builder.setSmallIcon(R.drawable.ic_launcher_background); | |
builder.setContentTitle("Test Notification"); | |
builder.setContentText("Hello world"); | |
builder.setAutoCancel(true); | |
builder.setDefaults(NotificationCompat.DEFAULT_ALL); | |
Uri path = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); | |
builder.setSound(path); | |
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); | |
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ | |
String channelId = "test"; | |
NotificationChannel channel = new NotificationChannel( channelId,"This is channel",NotificationManager.IMPORTANCE_DEFAULT); | |
notificationManager.createNotificationChannel(channel); | |
builder.setChannelId(channelId); | |
} | |
notificationManager.notify(notificationId,builder.build()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment