Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kobakei committed Dec 16, 2017
1 parent c4cbd97 commit ca6cdd0
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Expand Up @@ -19,6 +19,8 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service android:name=".SampleForegroundService"/>
</application>

</manifest>
Expand Up @@ -42,6 +42,10 @@ class MainActivity : AppCompatActivity() {
buttonBundled.setOnClickListener {
NotificationUtility.showBundledNotification(applicationContext)
}
buttonColorized.setOnClickListener {
//NotificationUtility.showColorizedNotification(applicationContext)
startService(SampleForegroundService.createIntent(applicationContext))
}
}

}
@@ -1,15 +1,15 @@
package io.github.kobakei.androidnotificationshowcase

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.*
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.graphics.Color
import android.os.Build
import android.support.v4.app.NotificationCompat
import android.support.v4.content.ContextCompat
import android.view.LayoutInflater
import android.widget.RemoteViews

/**
* 通知関連のユーティリティ
Expand Down Expand Up @@ -208,20 +208,24 @@ class NotificationUtility {
val intent = Intent(context, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)

val style = NotificationCompat.DecoratedCustomViewStyle()
val customView = RemoteViews(context.packageName, R.layout.custom_layout)

val notification = NotificationCompat.Builder(context, CHANNEL_ID_NORMAL)
val notificationBuilder = NotificationCompat.Builder(context, CHANNEL_ID_NORMAL)
.setContentTitle("This is title")
.setContentText("This is message")
.setTicker("This is ticker") // for legacy Android
.setStyle(style)
.setCustomContentView(customView)
.setSmallIcon(R.drawable.ic_notification)
.setLargeIcon(BitmapFactory.decodeResource(context.resources, R.mipmap.ic_launcher_round))
.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
.setDefaults(Notification.DEFAULT_ALL)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build()

val style = NotificationCompat.DecoratedCustomViewStyle()
style.setBuilder(notificationBuilder)
val notification = style.build()

val nm = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
nm.notify(1, notification)
}
Expand Down Expand Up @@ -344,5 +348,30 @@ class NotificationUtility {
nm.notify(3, notification2)
nm.notify(4, notification3)
}

/**
* 色付きの通知を作成する
*/
fun createColorizedNotification(service: Service): Notification {
val context = service.applicationContext
val intent = Intent(context, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)

val notification = NotificationCompat.Builder(context, NotificationUtility.CHANNEL_ID_NORMAL)
.setContentTitle("This is title")
.setContentText("This is message")
.setTicker("This is ticker") // for legacy Android
.setSmallIcon(R.drawable.ic_notification)
.setLargeIcon(BitmapFactory.decodeResource(context.resources, R.mipmap.ic_launcher_round))
.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
.setDefaults(Notification.DEFAULT_ALL)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setColor(Color.GREEN)
.setColorized(true)
.setPriority(Notification.PRIORITY_HIGH)
.build()
return notification
}
}
}
@@ -0,0 +1,30 @@
package io.github.kobakei.androidnotificationshowcase

import android.app.IntentService
import android.content.Context
import android.content.Intent

/**
* フォアグラウンドサービスの例
* 色付き通知を出すために必要
*
* Created by keisuke on 2017/12/16.
*/
class SampleForegroundService : IntentService("sample") {

companion object {
fun createIntent(context: Context): Intent =
Intent(context, SampleForegroundService::class.java)
}

override fun onHandleIntent(p0: Intent?) {
startForeground(1, NotificationUtility.createColorizedNotification(this))
try {
Thread.sleep(10 * 1000L)
} catch (e: InterruptedException) {
e.printStackTrace()
}
}


}
14 changes: 14 additions & 0 deletions app/src/main/res/layout/custom_layout.xml
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F00">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Hello custom view"
android:textColor="#FFF"/>

</RelativeLayout>
10 changes: 10 additions & 0 deletions app/src/main/res/layout/main_layout.xml
Expand Up @@ -121,6 +121,16 @@
android:text="Bundled"
android:textAllCaps="false" />

<Button
android:id="@+id/buttonColorized"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Colorized"
android:textAllCaps="false" />

</LinearLayout>

</ScrollView>

0 comments on commit ca6cdd0

Please sign in to comment.