android_Slip_6B

1. activity_main.xml (layout file for MainActivity):
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingTop="16dp"
    android:paddingRight="16dp"
    android:paddingBottom="16dp"
    tools:context=".MainActivity">

    <EditText
    android:id="@+id/editTextNotificationMessage"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter Notification Message" />

    <Button
    android:id="@+id/buttonSendNotification"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/editTextNotificationMessage"
    android:layout_marginTop="16dp"
    android:text="Send Notification" />
    </RelativeLayout>

1. MainActivity.java:
    import androidx.appcompat.app.AppCompatActivity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;

    public class MainActivity extends AppCompatActivity {

    private EditText editTextNotificationMessage;
    private Button buttonSendNotification;

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

    editTextNotificationMessage = findViewById(R.id.editTextNotificationMessage);
    buttonSendNotification = findViewById(R.id.buttonSendNotification);

    buttonSendNotification.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    sendNotification();
    }
    });
    }

    private void sendNotification() {
    String message = editTextNotificationMessage.getText().toString();

    NotificationService.sendNotification(this, message);
    } }

2. activity_second.xml (layout file for SecondActivity):
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingTop="16dp"
    android:paddingRight="16dp"
    android:paddingBottom="16dp"
    tools:context=".SecondActivity">

    <TextView
    android:id="@+id/textViewNotificationMessage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    </RelativeLayout>

2.SecondActivity.java:
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.TextView;

    public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    TextView textViewNotificationMessage = findViewById(R.id.textViewNotificationMessage);

    // Retrieve the notification message from intent
    String notificationMessage = getIntent().getStringExtra("notificationMessage");

    // Display the notification message
    if (notificationMessage != null) {
    textViewNotificationMessage.setText(notificationMessage);
    }
    }
    }
NotificationService.java:
    import android.app.Notification;
    import android.app.NotificationChannel;
    import android.app.NotificationManager;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Build;

    import androidx.core.app.NotificationCompat;

    public class NotificationService {

    private static final String CHANNEL_ID = "MyNotificationChannel";

    public static void sendNotification(Context context, String message) {
    // Create an explicit intent for SecondActivity
    Intent intent = new Intent(context, SecondActivity.class);
    intent.putExtra("notificationMessage", message);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

    // Create a PendingIntent for the notification
    int requestCode = 0;
    int flags = 0;
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
    .setSmallIcon(R.drawable.ic_notification)
    .setContentTitle("Notification Example")
    .setContentText(message)
    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
    .setContentIntent(NotificationActivity.getPendingIntent(context, intent, requestCode, flags))
    .setAutoCancel(true);

    // Create a notification channel for devices running Android Oreo and higher
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    CharSequence name = "My Notification Channel";
    String description = "Description";
    int importance = NotificationManager.IMPORTANCE_DEFAULT;
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
    channel.setDescription(description);
    NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
    notificationManager.createNotificationChannel(channel);
    }

    // Get the NotificationManager and notify the user
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, builder.build());
    }
    }

No comments:

Post a Comment