-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from AnelCC/COM_00004_Notifications
Com 00004 notifications
- Loading branch information
Showing
12 changed files
with
267 additions
and
5 deletions.
There are no files selected for viewing
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
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
126 changes: 126 additions & 0 deletions
126
app/src/main/java/com/anelcc/usercomunacation/notification/NotificationActivity.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package com.anelcc.usercomunacation.notification | ||
|
||
import android.app.NotificationChannel | ||
import android.app.NotificationManager | ||
import android.app.PendingIntent | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.graphics.BitmapFactory | ||
import android.os.Build | ||
import androidx.appcompat.app.AppCompatActivity | ||
import android.os.Bundle | ||
import android.view.View | ||
import androidx.core.app.NotificationCompat | ||
import com.anelcc.usercomunacation.R | ||
|
||
class NotificationActivity : AppCompatActivity(), View.OnClickListener { | ||
companion object { | ||
private val NOTIFY_ID = 1001 | ||
private val NOTIFY_CHANNEL = "MY_CHANNEL" | ||
} | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_notification) | ||
|
||
findViewById<View>(R.id.btn_simple_notification).setOnClickListener(this) | ||
findViewById<View>(R.id.btn_large_notification).setOnClickListener(this) | ||
findViewById<View>(R.id.btn_action_notification).setOnClickListener(this) | ||
|
||
// For API 26 and later, we have to create a channel otherwise the notification | ||
// won't be displayed. This can be called multiple times without harm - if there's | ||
// already a channel with the given ID then the call is ignored | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
val importance = NotificationManager.IMPORTANCE_DEFAULT | ||
val channel = NotificationChannel(NOTIFY_CHANNEL, "Notifications", importance) | ||
channel.description = "This is a notification channel" | ||
|
||
// Register the channel with the system | ||
val notificationManager = getSystemService(NotificationManager::class.java) | ||
notificationManager?.createNotificationChannel(channel) | ||
} | ||
} | ||
|
||
override fun onClick(v: View?) { | ||
when (v!!.id) { | ||
R.id.btn_simple_notification -> createNotification() | ||
R.id.btn_large_notification -> createLargeNotification() | ||
R.id.btn_action_notification -> createActionNotification() | ||
} | ||
} | ||
|
||
private fun createNotification() { | ||
val builder = NotificationCompat.Builder(this, NOTIFY_CHANNEL) | ||
// Set the three required items all notifications must have | ||
builder.setSmallIcon(R.drawable.ic_notifications_accent) | ||
builder.setContentTitle("Sample Notification") | ||
builder.setContentText("This is a sample Expanded Notification") | ||
// Set the notification to cancel when the user taps on it | ||
builder.setAutoCancel(true) | ||
// Set the large icon to be our app's launcher icon | ||
builder.setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.user_woman_icon)) | ||
|
||
// Build the finished notification and then display it to the user | ||
val notification = builder.build() | ||
val mgr = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | ||
mgr.notify(NOTIFY_ID, notification) | ||
|
||
} | ||
|
||
|
||
private fun createLargeNotification() { | ||
val builder = NotificationCompat.Builder(this, NOTIFY_CHANNEL) | ||
builder.setSmallIcon(R.drawable.ic_notifications_accent) | ||
builder.setContentTitle("Sample Large Notification") | ||
builder.setContentText("More text here. Expand!") | ||
builder.setAutoCancel(true) | ||
builder.setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.user_woman_icon)) | ||
// Add an expanded layout to the notification | ||
val bigTextStyle = NotificationCompat.BigTextStyle() | ||
bigTextStyle.setBigContentTitle("This is a Expand Notification") | ||
bigTextStyle.bigText(resources.getString(R.string.long_msg)) | ||
builder.setStyle(bigTextStyle) | ||
|
||
// Build the finished notification and then display it to the user | ||
val notification = builder.build() | ||
val mgr = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | ||
mgr.notify(NOTIFY_ID, notification) | ||
} | ||
|
||
private fun createActionNotification() { | ||
val builder = NotificationCompat.Builder(this, NOTIFY_CHANNEL) | ||
// Create the intent that will start the ResultActivity when the user | ||
// taps the notification or chooses an action button | ||
val intent = Intent(this, NotificationResultActivity::class.java) | ||
// Store the notification ID so we can cancel it later in the ResultActivity | ||
intent.putExtra("notifyID", NOTIFY_ID) | ||
val pendingIntent = PendingIntent.getActivity(this, NOTIFY_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT) | ||
|
||
builder.setSmallIcon(R.drawable.ic_notifications_accent) | ||
builder.setContentTitle("Sample Large Notification") | ||
builder.setContentText("More text here. Expand!") | ||
builder.setAutoCancel(true) | ||
builder.setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.user_woman_icon)) | ||
|
||
// Set the small subtext message | ||
builder.setSubText("Tap to view") | ||
// Set the content intent to launch our result activity | ||
builder.setContentIntent(pendingIntent) | ||
|
||
val bigTextStyle = NotificationCompat.BigTextStyle() | ||
bigTextStyle.setBigContentTitle("This is a Expand Notification") | ||
bigTextStyle.bigText(resources.getString(R.string.long_msg)) | ||
builder.setStyle(bigTextStyle) | ||
// Add action buttons to the Notification if they are supported | ||
// Use the same PendingIntent as we use for the main notification action | ||
builder.addAction(R.mipmap.ic_launcher, "Action 1", pendingIntent) | ||
builder.addAction(R.mipmap.ic_launcher, "Action 2", pendingIntent) | ||
|
||
// Set the lock screen visibility of the notification | ||
builder.setVisibility(NotificationCompat.VISIBILITY_SECRET) | ||
|
||
val notification = builder.build() | ||
val mgr = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | ||
mgr.notify(NOTIFY_ID, notification) | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
app/src/main/java/com/anelcc/usercomunacation/notification/NotificationResultActivity.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.anelcc.usercomunacation.notification | ||
|
||
import android.app.NotificationManager | ||
import android.content.Context | ||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
import com.anelcc.usercomunacation.R | ||
|
||
class NotificationResultActivity : AppCompatActivity() { | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_notification_result) | ||
|
||
// When launched from an addAction Intent, we must manually cancel | ||
// the notification otherwise it will stay in the status bar | ||
val intent = intent | ||
val notifyID = intent.getIntExtra("notifyID", 0) | ||
|
||
val mgr = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | ||
mgr.cancel(notifyID) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="24.0" | ||
android:viewportHeight="24.0"> | ||
<path | ||
android:fillColor="#CE93D8" | ||
android:pathData="M12,22c1.1,0 2,-0.9 2,-2h-4c0,1.1 0.89,2 2,2zM18,16v-5c0,-3.07 -1.64,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68C7.63,5.36 6,7.92 6,11v5l-2,2v1h16v-1l-2,-2z"/> | ||
</vector> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout | ||
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" | ||
android:padding="@dimen/small_padding" | ||
tools:context=".notification.NotificationActivity" | ||
android:background="@color/colorOrangeBackground"> | ||
|
||
<Button | ||
android:id="@+id/btn_simple_notification" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_alignParentStart="true" | ||
android:layout_alignParentTop="true" | ||
android:layout_marginBottom="8dp" | ||
android:background="@color/colorAccent" | ||
android:text="@string/simple_notification" | ||
app:layout_constraintBottom_toTopOf="@+id/btn_large_notification" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" /> | ||
|
||
<Button | ||
android:id="@+id/btn_large_notification" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_alignParentStart="true" | ||
android:layout_alignParentTop="true" | ||
android:background="@color/colorAccent" | ||
android:text="@string/large_notification" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
|
||
<Button | ||
android:id="@+id/btn_action_notification" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_alignParentStart="true" | ||
android:layout_alignParentTop="true" | ||
android:layout_marginTop="@dimen/small_margin" | ||
android:background="@color/colorAccent" | ||
android:text="@string/action_notification" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@+id/btn_large_notification" /> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout | ||
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" | ||
android:padding="@dimen/normal_padding" | ||
tools:context=".notification.NotificationActivity" | ||
android:background="@color/colorGreenBackground"> | ||
|
||
<TextView | ||
android:id="@+id/notification_result" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_alignParentStart="true" | ||
android:layout_alignParentLeft="true" | ||
android:layout_alignParentTop="true" | ||
android:text="This activity was launched as the result of a Notification" | ||
android:textAppearance="?android:attr/textAppearanceLarge" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> |
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
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
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