Skip to content

Commit

Permalink
Merge pull request #4 from AnelCC/COM_00004_Notifications
Browse files Browse the repository at this point in the history
Com 00004 notifications
  • Loading branch information
AnelCC committed May 6, 2020
2 parents 703d0c7 + 775bdc3 commit 31685d8
Show file tree
Hide file tree
Showing 12 changed files with 267 additions and 5 deletions.
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".dialog.DialogActivity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand All @@ -19,6 +18,9 @@
</activity>
<activity android:name=".toast.ToastActivity" />
<activity android:name=".snackbar.SnackbarActivity" />
<activity android:name=".dialog.DialogActivity" />
<activity android:name=".notification.NotificationActivity" />
<activity android:name=".notification.NotificationResultActivity" />
</application>

</manifest>
3 changes: 3 additions & 0 deletions app/src/main/java/com/anelcc/usercomunacation/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.os.Bundle
import android.util.Log
import android.view.View
import com.anelcc.usercomunacation.dialog.DialogActivity
import com.anelcc.usercomunacation.notification.NotificationActivity
import com.anelcc.usercomunacation.snackbar.SnackbarActivity
import com.anelcc.usercomunacation.toast.ToastActivity

Expand All @@ -18,13 +19,15 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
findViewById<View>(R.id.activity_launch_toast).setOnClickListener(this)
findViewById<View>(R.id.activity_launch_snackbar).setOnClickListener(this)
findViewById<View>(R.id.activity_launch_dialog).setOnClickListener(this)
findViewById<View>(R.id.activity_launch_notification).setOnClickListener(this)
}

override fun onClick(v: View) {
when (v.id) {
R.id.activity_launch_toast -> startActivity(Intent(this, ToastActivity::class.java))
R.id.activity_launch_snackbar -> startActivity(Intent(this, SnackbarActivity::class.java))
R.id.activity_launch_dialog -> startActivity(Intent(this, DialogActivity::class.java))
R.id.activity_launch_notification -> startActivity(Intent(this, NotificationActivity::class.java))
}
}
}
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)
}
}
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)
}
}
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_notifications_accent.xml
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>
Binary file added app/src/main/res/drawable/user_woman_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 15 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<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"
Expand Down Expand Up @@ -50,4 +51,17 @@
app:layout_constraintStart_toStartOf="@+id/activity_launch_snackbar"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/activity_launch_notification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:layout_below="@+id/activity_launch_dialog"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/small_margin"
android:text="@string/launch_notification_activity"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/activity_launch_dialog" />

</androidx.constraintlayout.widget.ConstraintLayout>
51 changes: 51 additions & 0 deletions app/src/main/res/layout/activity_notification.xml
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>
26 changes: 26 additions & 0 deletions app/src/main/res/layout/activity_notification_result.xml
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>
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_toast.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
android:layout_height="match_parent"
android:padding="@dimen/small_margin"
tools:context=".toast.ToastActivity"
android:background="@color/colorGreenBackground">
android:background="@color/colorCyanBackground">

<TextView
android:id="@+id/toast_time"
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
<color name="colorWhite">#FFFFFF</color>

<color name="colorPurpleBackground">#E1BEE7</color>
<color name="colorGreenBackground">#B3E5FC</color>
<color name="colorCyanBackground">#B3E5FC</color>
<color name="colorBlueBackground">#C5CAE9</color>
<color name="colorYellowBackground">#FFF9C4</color>
<color name="colorOrangeBackground">#FFCCBC</color>
<color name="colorGreenBackground">#DCEDC8</color>
</resources>
8 changes: 7 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<resources>
<string name="app_name">UserComunacation</string>
<string name="app_name">Tiny Messages</string>
<string name="launch_dialog_activity">Launch Dialog Activity</string>
<string name="launch_toast_activity">Launch Toast Activity</string>
<string name="launch_notification_activity">Launch Notification Activity</string>
Expand All @@ -15,4 +15,10 @@
<string name="show_custom_layout">Show Custom Layout</string>
<string name="FirstNameHint">First name</string>
<string name="LastNameHint">Last name</string>
<string name="simple_notification">Show Simple Notification</string>
<string name="large_notification">Show Large Notification</string>
<string name="action_notification">Show Action Notification</string>
<string name="notification_news">This activity was launched as the result of a Notification</string>
<string name="long_msg">This is a notification style that can contain a large amount of text. It works really well when you have a large amount of information
to display in the notification area and want the user to be able to expand the message to see all of the content.</string>
</resources>

0 comments on commit 31685d8

Please sign in to comment.