Skip to content

Instantly share code, notes, and snippets.

@alwarren
Last active August 9, 2018 20:27
Show Gist options
  • Select an option

  • Save alwarren/7babf892d5de5f6398a88c267c811c1b to your computer and use it in GitHub Desktop.

Select an option

Save alwarren/7babf892d5de5f6398a88c267c811c1b to your computer and use it in GitHub Desktop.
An Updated Notification Example
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import kotlinx.android.synthetic.main.activity_main.*
import android.app.AlarmManager
import android.app.Notification
import android.os.SystemClock
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.app.NotificationManager
import android.app.NotificationChannel
import android.os.Build
import android.support.v4.app.NotificationCompat
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
// Create a NotificationChannel if needed for API 26+
createNotificationChannel()
fab.setOnClickListener { startActivity(Intent(this, MessageActivity::class.java)) }
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.action_5 -> {
scheduleNotification(getNotification(getString(R.string.delay_5_seconds)),
DELAY_5_SECONDS)
return true
}
R.id.action_10 -> {
scheduleNotification(getNotification(getString(R.string.delay_10_seconds)),
DELAY_10_SECONDS)
return true
}
R.id.action_30 -> {
scheduleNotification(getNotification(getString(R.string.delay_30_seconds)),
DELAY_30_SECONDS)
return true
}
else -> super.onOptionsItemSelected(item)
}
}
/**
* Schedule a notification using an alarm
*
* @param notification Notification - used by the alarm to send a notification
* @param delaySeconds Int - delay in seconds before sending the notification
* @return Unit
*/
private fun scheduleNotification(notification: Notification, delaySeconds: Int) {
// create an intent for the broadcast receiver
val notificationIntent = Intent(this, NotificationReceiver::class.java)
notificationIntent.putExtra(NotificationReceiver.NOTIFICATION_ID, 1)
notificationIntent.putExtra(NotificationReceiver.NOTIFICATION, notification)
// create a pending intent for the alarm
val pendingIntent = PendingIntent.getBroadcast(this, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT)
val futureInMillis = SystemClock.elapsedRealtime() + delaySeconds * 1000
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent)
}
/**
* Get a Notification for the alarm
*
* @param message String - sent from the notification to the MessageActivity
* @return Notification - used by the alarm to send a notification
*/
private fun getNotification(message: String): Notification {
// create a pending intent for the notification action
// message will be added to the pending intent and received by MessageActivity
val actionIntent = MessageActivity.notificationIntent(this, message)
return NotificationCompat.Builder(this,
MessageActivity.NOTIFICATION_CHANNEL_ID)
// set the pending intent for the notification action
// (used to launch MessageActivity when the notification is clicked)
.setContentIntent(actionIntent)
// remove the notification from the app bar when clicked
.setAutoCancel(true)
.setContentTitle(getString(R.string.notification_title))
.setContentText(message)
.setSmallIcon(R.drawable.ic_announcement_white_24dp)
.build()
}
/**
* Create a notification channel for versions >= Oreo
*/
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = getString(R.string.channel_name)
val description = getString(R.string.channel_description)
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(MessageActivity.NOTIFICATION_CHANNEL_ID, name, importance)
channel.description = description
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
val notificationManager = getSystemService(NotificationManager::class.java)
notificationManager!!.createNotificationChannel(channel)
}
}
}
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_message.*
import kotlinx.android.synthetic.main.content_message.*
class MessageActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_message)
setSupportActionBar(toolbarTarget)
val message: String? = intent.getStringExtra(MESSAGE_KEY)
?: getString(R.string.message_not_received)
textViewMessage.text = String.format(resources.getString(R.string.message), message)
}
companion object {
const val NOTIFICATION_CHANNEL_ID = "ntxdroid.com.notifications.MessageActivity"
private const val PENDING_INTENT_ID = 2001
const val MESSAGE_KEY = "MESSAGE_KEY"
fun notificationIntent(context: Context, message: String): PendingIntent {
val startActivityIntent = Intent(context, MessageActivity::class.java)
startActivityIntent.putExtra(MESSAGE_KEY, message)
return PendingIntent.getActivity(
context,
PENDING_INTENT_ID,
startActivityIntent,
PendingIntent.FLAG_UPDATE_CURRENT)
}
}
}
import android.app.Notification
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.app.NotificationManager
class NotificationReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notification = intent.getParcelableExtra<Notification>(NOTIFICATION)
val id = intent.getIntExtra(NOTIFICATION_ID, 0)
notificationManager.notify(id, notification)
}
companion object {
var NOTIFICATION_ID = "notification-id"
var NOTIFICATION = "notification"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment