Created
March 25, 2020 16:59
-
-
Save nandunbandara/c5aace9afa3a7ec13c02c7754c5dc4bb to your computer and use it in GitHub Desktop.
MAD Labsheet 4
This file contains 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 lk.sliit.mad.labsheet4; | |
import androidx.appcompat.app.AppCompatActivity; | |
import androidx.core.app.NotificationCompat; | |
import androidx.core.app.NotificationManagerCompat; | |
import android.app.NotificationChannel; | |
import android.app.NotificationManager; | |
import android.app.PendingIntent; | |
import android.content.Intent; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import org.w3c.dom.Text; | |
public class MainActivity extends AppCompatActivity { | |
/** | |
* Channel ID to be used to send notifications | |
*/ | |
private final String CHANNEL_ID = "111"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// create notification channel if API version is greater than 26 | |
this.createNotificationChannel(); | |
// Get signup button reference | |
Button signUpButton = (Button) findViewById(R.id.btnSignUp); | |
/** | |
* Add listener to signup button to show a notification | |
* on button click | |
*/ | |
signUpButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
EditText txtName = findViewById(R.id.txtName); | |
String name = txtName.getText().toString(); | |
if (!name.isEmpty()) { | |
showNotification(name); | |
} else { | |
txtName.setError("Please enter a name"); | |
} | |
} | |
}); | |
} | |
/** | |
* Function to create the notification channel | |
* This is only for API version 26 and up | |
* | |
* A channel is through with the notifications are sent. | |
* It's like a group or conversation on whatsapp where messages from | |
* participants in it are sent through either that specific group or | |
* conversation only | |
*/ | |
private void createNotificationChannel() { | |
// Check if API version is greater than 26 | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
// Define channel properties | |
CharSequence name = getString(R.string.notifications_channel_name); | |
String description = getString(R.string.notifications_channel_description); | |
int importance = NotificationManager.IMPORTANCE_DEFAULT; | |
// Create new notification channel object and set properties | |
NotificationChannel channel = new NotificationChannel( | |
CHANNEL_ID, | |
name, | |
importance | |
); | |
channel.setDescription(description); | |
// Register channel with the notification manager | |
NotificationManager notificationManager = getSystemService(NotificationManager.class); | |
notificationManager.createNotificationChannel(channel); | |
} | |
} | |
/** | |
* Function to show push notification when the button is clicked | |
* This is called within the listener for the button | |
* | |
* @param name - The value entered by the user in the text input (EditText) | |
*/ | |
private void showNotification(String name) { | |
// Create intent to the registration form activity | |
Intent registrationIntent = new Intent(this, Registration.class); | |
/** | |
* Check out this link for explanation on the following flags | |
* https://stackoverflow.com/questions/21833402/difference-between-intent-flag-activity-clear-task-and-intent-flag-activity-task/29565717 | |
*/ | |
registrationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); | |
/** | |
* A pending intent specifies an action that will take place in the future. According to | |
* this usecase, this intent will be fired only when the push notification is clicked | |
*/ | |
PendingIntent pendingRegistrationIntent = PendingIntent.getActivity(this, 0, registrationIntent, 0); | |
/** | |
* Build the push notification | |
* NOTE: you have to use the same channel id used above in the code | |
*/ | |
NotificationCompat.Builder builder = | |
new NotificationCompat.Builder(this, CHANNEL_ID) | |
.setSmallIcon(R.drawable.ic_launcher_background) | |
.setContentTitle("My notification") | |
.setContentText("Hello ".concat(name).concat("!, welcome to the MAD team")) | |
.setPriority(NotificationCompat.PRIORITY_DEFAULT) | |
.setContentIntent(pendingRegistrationIntent) | |
.setAutoCancel(true); | |
/** | |
* Send notification | |
*/ | |
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); | |
notificationManager.notify(0, builder.build()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment