Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- Intent notifIntent = new Intent(this,MainActivity.class);
- PendingIntent pendingIntent = PendingIntent.getActivity(this,
- 0,notifIntent,0);
- notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- contentView = new RemoteViews(getPackageName(), R.layout.download_notification_bar);
- contentView.setImageViewResource(R.id.image, R.mipmap.ic_launcher);
- contentView.setTextViewText(R.id.title, "Custom notification");
- notification = new NotificationCompat.Builder(this,CHANEL_ID)
- .setContentTitle("test")
- .setContentText("test Againg")
- .setContent(contentView)
- .setSmallIcon(R.drawable.ic_launcher_foreground)
- .setContentIntent(pendingIntent)
- .build();
- startForeground(1,notification);
- return START_STICKY;
- }
- <!-- Android Q requirement -->
- <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
- class ForegroundServiceSample : Service() {
- companion object {
- @JvmStatic
- fun start(context: Context) {
- ContextCompat.startForegroundService(context, Intent(context, ForegroundServiceSample::class.java))
- }
- @JvmStatic
- fun stop(context: Context) {
- context.stopService(Intent(context, ForegroundServiceSample::class.java))
- }
- }
- // Foreground service notification =========
- private val foregroundNotificationId: Int = (System.currentTimeMillis() % 10000).toInt()
- private val foregroundNotification by lazy {
- NotificationCompat.Builder(this, foregroundNotificationChannelId)
- .setSmallIcon(R.drawable.ic_sample_service)
- .setPriority(NotificationCompat.PRIORITY_MIN)
- .setSound(null)
- .build()
- }
- private val foregroundNotificationChannelName by lazy {
- getString(R.string.sample_service_name)
- }
- private val foregroundNotificationChannelDescription by lazy {
- getString(R.string.sample_service_description)
- }
- private val foregroundNotificationChannelId by lazy {
- "ForegroundServiceSample.NotificationChannel".also {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- (getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).apply {
- if (getNotificationChannel(it) == null) {
- createNotificationChannel(NotificationChannel(
- it,
- foregroundNotificationChannelName,
- NotificationManager.IMPORTANCE_MIN
- ).also {
- it.description = foregroundNotificationChannelDescription
- it.lockscreenVisibility = NotificationCompat.VISIBILITY_PRIVATE
- it.vibrationPattern = null
- it.setSound(null, null)
- it.setShowBadge(false)
- })
- }
- }
- }
- }
- }
- // Lifecycle ===============================
- override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- startForeground(foregroundNotificationId, foregroundNotification)
- }
- return START_STICKY
- }
- override fun onBind(intent: Intent?): IBinder? {
- return null
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement