2018年5月16日 星期三

[Android] [Kotlin] Add Button in Notification

First, make a notification in your NotificationActivity.kt

@RequiresApi(Build.VERSION_CODES.O)
private fun makeNotificationShow(DateType: Int, iconID: Bitmap, title: String, text: String?, dataValue: Int) {
    val bigStyle = NotificationCompat.BigTextStyle()
    bigStyle.bigText(text)
    val intent = Intent(m_context!!, MainActivity::class.java)
    // The stack builder object will contain an artificial back stack for the
    // started Activity.    
    // This ensures that navigating backward from the Activity leads out of    
    // your application to the Home screen.    
    val stackBuilder = TaskStackBuilder.create(m_context)
    // Adds the back stack for the Intent (but not the Intent itself)    
    stackBuilder.addParentStack(MainActivity::class.java)
    // Adds the Intent that starts the Activity to the top of the stack    
    stackBuilder.addNextIntent(intent)
    val intentAction = Intent(m_context!!, NotificationButtonReceiver::class.java)
    val intentAction2 = Intent(m_context!!, NotificationButtonReceiver::class.java)
    //This is optional if you have more than one buttons and want to differentiate between two
    intentAction.action = "action1"    
    val pi = PendingIntent.getBroadcast(m_context!!, DateType, intentAction,0 )
    intentAction2.action = "action2"    
    val pi2 = PendingIntent.getBroadcast(m_context!!, DateType, intentAction2, 0)
    val notification = NotificationCompat.Builder(m_context)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setLargeIcon(iconID)
        .setContentTitle(title)
        .setStyle(NotificationCompat.BigTextStyle()
                .bigText(title))
        .setPriority(NotificationCompat.PRIORITY_MAX)
        .setCategory(NotificationCompat.CATEGORY_ALARM)
    // 0-no icon
        .addAction (0, m_context!!.getString(R.string.remindDismiss), pi)
        .addAction (0, m_context!!.getString(R.string.remindAfter_5_Mins), pi2)
        .setAutoCancel(true)
        .build()
    notification.contentIntent = pi
    
    // If Android > 7.0
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    val notificationHelper = NotificationHelper(m_context!!)
    notificationHelper.set_TCOC_Value(dataValue)
    val action1 = Notification.Action(0, m_context!!.getString(R.string.remindDismiss), pi)
    val action2 = Notification.Action(0, m_context!!.getString(R.string.remindAfter_5_Mins), pi2)
    val NB = notificationHelper.getNotification1(title, text.toString())
            .addAction(action1)
            .addAction(action2)
            .setAutoCancel(true)
    notificationHelper.notify(DateType, NB)
}

Next, add NotificationButtonReceiver.kt

class NotificationButtonReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {

        val date = Date().time        
        when(intent.action)
        {
            "action1"->{
             performAction1(context,date)
            }
            "action2"->{
             performAction2(context,date)
            }

        }
        //This is used to close the notification tray        
        //   val it = Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)        
        //   context.sendBroadcast(it)        
        val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        mNotificationManager.cancel(0x01)
        mNotificationManager.cancel(0x02)
    }

    fun performAction1(context: Context,nowTime:Long) {//設定明日再提醒        
        val share = context.getSharedPreferences("NotificationAction", Activity.MODE_PRIVATE)
        share.edit().putString("nextNotification", "tomorrow").apply()
        share.edit().putString("now time", nowTime.toString()).apply()
        Log.d("NotificationButton","action 1")
    }

    fun performAction2(context: Context,nowTime:Long) {//設定五分鐘提醒        
        val share = context.getSharedPreferences("NotificationAction", Activity.MODE_PRIVATE)
        share.edit().putString("nextNotification", "5min" ).apply()
        share.edit().putString("now time", nowTime.toString()).apply()
        Log.d("NotificationButton","action 2")
    }

}



沒有留言:

張貼留言