AndroidManifest.xml
< ?xml version="1.0" encoding="utf-8"?>
< manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.firstapp">
< application
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>
activity_main.xml
< ?xml version="1.0" encoding="utf-8"?>
< RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
< TextView
android:id="@+id/mytext"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:typeface="serif"
android:layout_above="@id/mybtn"
android:gravity="center"
android:textStyle="bold"
android:textSize="20sp"
android:text="Android Notification"
android:inputType="textMultiLine" />
< Button
android:id="@+id/mybtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:padding="20dp"
android:layout_alignParentBottom="true"
android:text="Press Me" />
< /RelativeLayout>
MainActivity.java
package com.example.firstapp;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.app.Notification;
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;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v) {
sendNotification();
}
private void sendNotification() {
Intent in=new Intent(this,MainActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this,
(int)System.currentTimeMillis(),in,0);
Uri soundUri = RingtoneManager.
getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification notification=new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_menu_camera)
.setContentTitle("Notification Tutorial")
.setContentText("This is first Notification")
.setContentIntent(pendingIntent)
.setSound(soundUri)
.setDefaults(Notification.DEFAULT_VIBRATE)
.setAutoCancel(true)
.build();
NotificationManager notificationManager=(NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0,notification);
}
}


No comments:
Post a Comment