diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3d92332..39bc665 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,7 +9,6 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> - @@ -19,6 +18,9 @@ + + + \ No newline at end of file diff --git a/app/src/main/java/com/anelcc/usercomunacation/MainActivity.kt b/app/src/main/java/com/anelcc/usercomunacation/MainActivity.kt index a156f30..cf3a39f 100644 --- a/app/src/main/java/com/anelcc/usercomunacation/MainActivity.kt +++ b/app/src/main/java/com/anelcc/usercomunacation/MainActivity.kt @@ -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 @@ -18,6 +19,7 @@ class MainActivity : AppCompatActivity(), View.OnClickListener { findViewById(R.id.activity_launch_toast).setOnClickListener(this) findViewById(R.id.activity_launch_snackbar).setOnClickListener(this) findViewById(R.id.activity_launch_dialog).setOnClickListener(this) + findViewById(R.id.activity_launch_notification).setOnClickListener(this) } override fun onClick(v: View) { @@ -25,6 +27,7 @@ class MainActivity : AppCompatActivity(), View.OnClickListener { 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)) } } } diff --git a/app/src/main/java/com/anelcc/usercomunacation/notification/NotificationActivity.kt b/app/src/main/java/com/anelcc/usercomunacation/notification/NotificationActivity.kt new file mode 100644 index 0000000..710cba3 --- /dev/null +++ b/app/src/main/java/com/anelcc/usercomunacation/notification/NotificationActivity.kt @@ -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(R.id.btn_simple_notification).setOnClickListener(this) + findViewById(R.id.btn_large_notification).setOnClickListener(this) + findViewById(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) + } +} diff --git a/app/src/main/java/com/anelcc/usercomunacation/notification/NotificationResultActivity.kt b/app/src/main/java/com/anelcc/usercomunacation/notification/NotificationResultActivity.kt new file mode 100644 index 0000000..95899b9 --- /dev/null +++ b/app/src/main/java/com/anelcc/usercomunacation/notification/NotificationResultActivity.kt @@ -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) + } +} diff --git a/app/src/main/res/drawable/ic_notifications_accent.xml b/app/src/main/res/drawable/ic_notifications_accent.xml new file mode 100644 index 0000000..8e70c1b --- /dev/null +++ b/app/src/main/res/drawable/ic_notifications_accent.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/user_woman_icon.png b/app/src/main/res/drawable/user_woman_icon.png new file mode 100644 index 0000000..aae7910 Binary files /dev/null and b/app/src/main/res/drawable/user_woman_icon.png differ diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index a6256d8..7aaa463 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,5 +1,6 @@ - +