【问题标题】:Why does my notification show every time I open my Android app?为什么每次打开我的 Android 应用程序时都会显示我的通知?
【发布时间】:2021-10-02 04:05:48
【问题描述】:

我设置了这段代码(或者我是这么认为的),以便在用户的 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>

【问题讨论】:

  • 我不知道 kotlin,但是通过尝试找出您的代码,我想我可以看到问题所在。每当您打开应用程序时,您都会定义一个通知,然后将其设置为提前 1 周。下次打开应用程序时,您不会检查是否有待处理的通知,您只需重复该过程
  • 很确定通知渠道和 id 可以防止这种情况:val notificationChannel = NotificationChannel(notChanId, "My Notifications", NotificationManager.IMPORTANCE_MAX) 此外,它发生在应用程序的第一次运行时。我认为是来自notificationManager.notify(1, notification) 被调用,但我不知道为什么会在触发警报之前。
  • 它在设备运行时阻止它。重新启动后,它会创建一个新的。第一次运行时,会显示通知。第二次,由于使用相同的ID而更新。设备重启后,没有更新通知,所以它正在创建一个新的
  • 可能是timeInMillis = System.currentTimeMillis()的原因,能不能换成set(Calendar.DAY_OF_YEAR, 1)

标签: android kotlin push-notification notifications


【解决方案1】:

你读过the documentation on AlarmManager.setRepeating吗?

如果规定的触发时间是过去,警报将立即触发,警报计数取决于触发时间相对于重复间隔过去多长时间。 p>

我怀疑这是你问题的根源。

如果是这样,那么解决方案就是确保将triggerAtMillis 设置为未来的某个时间。可能通过一些额外/不同的Calendar 调用是可行的。

【讨论】:

  • 啊,我错过了!我会解决这个问题,看看是否是问题所在。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多