为什么每次打开android应用程序时都会显示我的通知?

camsedfj  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(1145)

我将此代码设置为(或者我认为是这样)在用户的android设备上每7天显示一次通知,即使用户重新启动设备。但我看到的副作用是,每次用户打开应用程序时,都会弹出通知。
如何进行设置,以便即使在重新启动后也能正确设置通知,但直到要求的时间才显示通知。
我有一个 MainActivity 为此:

override fun onCreate(savedInstanceState: Bundle?) {
  val bootRec = BootReceiver()
  bootRec.scheduleNotifications(this)
}

它叫 BootReceiver 为此:

class BootReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        if (intent.action == "android.intent.action.BOOT_COMPLETED") {
            scheduleNotifications(context)
        }
    }

    fun scheduleNotifications(context: Context) {
        val receiver = ComponentName(context, BootReceiver::class.java)

        context.packageManager.setComponentEnabledSetting(
            receiver,
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
            PackageManager.DONT_KILL_APP
        )

        val alarmMgr: AlarmManager?

        alarmMgr = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
        val alarmIntent: PendingIntent = Intent(context, AlarmReceiver::class.java).let { intent ->
            PendingIntent.getBroadcast(context, 0, intent, 0)
        }

        // Set the alarm to start at 8:30 a.m.
        val calendar: Calendar = Calendar.getInstance().apply {
            timeInMillis = System.currentTimeMillis()
            set(Calendar.DAY_OF_WEEK, 1)
            set(Calendar.HOUR_OF_DAY, 15)
            set(Calendar.MINUTE, 13)
        }

        // setRepeating() lets you specify a precise custom interval--in this case,
        // 1 week.
        alarmMgr.setRepeating(
            AlarmManager.RTC_WAKEUP,
            calendar.timeInMillis,
            AlarmManager.INTERVAL_DAY * 7,
            alarmIntent
        )
    }
}

这反过来又叫 AlarmReceiver :

class AlarmReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        val notificationManager = context.getSystemService(AppCompatActivity.NOTIFICATION_SERVICE) as NotificationManager
        val notChanId = "icollecteverything"
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val notificationChannel = NotificationChannel(notChanId, "My Notifications", NotificationManager.IMPORTANCE_MAX)
            notificationChannel.enableLights(true)
            notificationChannel.lightColor = Color.BLUE
            notificationChannel.vibrationPattern = longArrayOf(0, 1000, 500, 1000)
            notificationChannel.enableVibration(true)
            notificationManager.createNotificationChannel(notificationChannel)
        }

        // Create an explicit intent for an Activity in your app
        val intentMain = Intent(context, MainActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        }
        val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, intentMain, 0)

        val notification = NotificationCompat.Builder(context, notChanId)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setPriority(NotificationManagerCompat.IMPORTANCE_HIGH)
            .setContentIntent(pendingIntent)
            .setContentTitle("Title")
            .setContentText("Notification Text.")
            .setStyle(
                NotificationCompat.BigTextStyle()
                    .bigText("Notification Text."))
            .build()
        notificationManager.notify(1, notification)
    }

}

这个 AndroidManifest 其中包括:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<receiver android:name=".helper.AlarmReceiver" android:enabled="true"/>
<receiver android:name=".helper.BootReceiver"
        android:exported="true"
        android:enabled="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
zpqajqem

zpqajqem1#

你有没有读过这本书的文档 AlarmManager.setRepeating ?
如果规定的触发时间在过去,则会立即触发报警,报警计数取决于触发时间过去与重复间隔之间的距离。
我怀疑这是你问题的根源。
如果是这样,那么解决办法就是确保 triggerAtMillis 设定为将来的某个时间。可能通过一些额外的/不同的 Calendar 电话。

相关问题