How to stop getting notification on every launch of application in kotlin?

package com.greenhcm.android

import android.app.*
import android.app.Notification.*
import android.content.BroadcastReceiver
import android.content.ComponentName
import android.content.Context
import android.content.Context.MODE_PRIVATE
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.BitmapFactory
import android.graphics.Color
import android.os.Bundle
import androidx.core.app.NotificationCompat
import kotlinx.android.synthetic.main.fragment_profile.*


class AlarmBroadcastReceiver: BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        showNotification(context)

    }


    internal fun showNotification(context: Context) {
        val CHANNEL_ID = "1"// The id of the channel.
        val name = context.getResources().getString(R.string.app_name)// The user-visible name of the channel.
        val mBuilder: NotificationCompat.Builder

        val intent = Intent()
        val manufacturer = android.os.Build.MANUFACTURER
        when(manufacturer) {
            "xiaomi" ->
                intent.component=
                    ComponentName("com.miui.securitycenter",
                        "com.miui.permcenter.autostart.AutoStartManagementActivity")
            "oppo" ->
                intent.component =
                    ComponentName("com.coloros.safecenter",
                        "com.coloros.safecenter.permission.startup.StartupAppListActivity")
            "vivo" ->
                intent.component =
                    ComponentName("com.vivo.permissionmanager",
                        "com.vivo.permissionmanager.activity.BgStartUpManagerActivity")
            "samsung" ->
            intent.component =
                ComponentName(
                    "com.samsung.android.lool",
                    "com.samsung.android.sm.ui.battery.BatteryActivity")

            "asus" ->
            intent.component =
                ComponentName(
                    "com.asus.mobilemanager",
                    "com.asus.mobilemanager.MainActivity"
                )
        }

        val list = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
        if (list.size > 0) {
            context.startActivity(intent)
        }


        val notificationIntent = Intent(context, GreenHCMActivity::class.java)
        val bundle = Bundle()
        notificationIntent.putExtras(bundle)

        //notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK)

        //val contentIntent = PendingIntent.getActivity(
          //  context,
            //1,
            //notificationIntent,
            //PendingIntent.FLAG_UPDATE_CURRENT
        //)

        var contentIntent = PendingIntent.getActivity(context, 1, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT)


        val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        //mNotificationManager.cancel(1)

        mNotificationManager.cancelAll()
        if (android.os.Build.VERSION.SDK_INT >= 26)
        {

            val mChannel = NotificationChannel(
                CHANNEL_ID,
                name,
                NotificationManager.IMPORTANCE_HIGH


            )




            mNotificationManager.createNotificationChannel(mChannel)

            mBuilder = NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLights(Color.RED, 300, 300)
                .setChannelId(CHANNEL_ID)
                 .setDefaults(DEFAULT_ALL)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setVibrate(longArrayOf(1000, 1000, 1000, 1000, 1000))
                .setAutoCancel(true)
                // Overrides ContentTitle in the big form of the template.
                .setContentTitle("Selamat Pagi Jangan Lupa Untuk Absensi ")
                .setContentIntent(contentIntent);

                //.setContentTitle("Selamat Pagi Jangan Lupa Untuk Absensi ")


        }


        else
        {
            mBuilder = NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setPriority(Notification.PRIORITY_HIGH)
                //.setPriority(NotificationCompat.PRIORITY_MAX)
                .setContentTitle("Title")

        }
        mBuilder.setContentIntent(contentIntent)
            .setDefaults(DEFAULT_ALL)
        mBuilder.setSmallIcon(R.mipmap.ic_launcher_round)
        val note = mBuilder.build()
       mBuilder.setContentText("Mohon melakukan absensi pagi ini,Terima kasih")


           .setStyle(
               NotificationCompat.BigPictureStyle()

                   // Provide the bitmap to be used as the payload for the BigPicture notification.
                   .bigPicture(BitmapFactory.decodeResource(context.resources, R.drawable.absence))

                   // Override the large icon when the big notification is shown.
                   .bigLargeIcon(
                       BitmapFactory.decodeResource(
                           context.resources,
                           R.mipmap.ic_launcher_round
                       )
                   )


               // Set the first line of text after the detail section in the big form of the template.
               //.setSummaryText("Mohon melakukan absensi pagi ini,Terima kasih")

           )
        mNotificationManager.notify(1, mBuilder.build())



    }

    



}

Above My Receiver like this,

Below in oncreate activity

val _intent = Intent(this, AlarmBroadcastReceiver::class.java)
        val pendingIntent = PendingIntent.getBroadcast(this, 1, _intent, 0)
        val alarmManager = this.getSystemService(Context.ALARM_SERVICE) as AlarmManager
        val calendar = Calendar.getInstance()
        calendar.setTimeInMillis(System.currentTimeMillis())
        calendar.set(Calendar.HOUR_OF_DAY, 7)
        calendar.set(Calendar.MINUTE, 0)
        calendar.set(Calendar.SECOND, 0)
        calendar.set(Calendar.AM_PM,Calendar.AM);
        //alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis() , 24*60*60*1000, pendingIntent)
        //alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis()/1000 ,AlarmManager.INTERVAL_DAY, pendingIntent)
        //alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis()/1000 ,AlarmManager.INTERVAL_DAY, pendingIntent)
        //alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis()/1000 ,AlarmManager.INTERVAL_DAY , pendingIntent)
        //alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent)
        alarmManager.cancel(pendingIntent)
        alarmManager.setInexactRepeating(
            AlarmManager.RTC_WAKEUP,
            calendar.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY,
            pendingIntent
        )

I'm new to android development... trying to build application which send notification at specific time daily. But after that if I open application it shows notification every time on launch.. how to stop showing it again? please help me thank you



Read more here: https://stackoverflow.com/questions/66326765/how-to-stop-getting-notification-on-every-launch-of-application-in-kotlin

Content Attribution

This content was originally published by Iqbal Alhadad at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: