在Android中关闭应用程序时显示通知

时间:2020-11-05 18:53:52

标签: java android kotlin android-notifications alarmmanager

即使该应用已关闭/在后台/前台,我也需要在特定日期显示本地通知。

当应用程序处于前台或后台时,我成功显示了通知。但是,如果应用程序已关闭,则什么也没有发生。

在此示例中,当我单击按钮时,我在2020年11月8日15时显示警报。 这是我的代码:

  1. 具有BroadCastReceiver的类接收器
class Receiver: BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent?) {
        val reqCode = 0
        val intent =  Intent(context, MainActivity::class.java)
        showNotification(context, "Title", "This is the message to display", intent, reqCode)
    }


    fun showNotification(context: Context, title: String?, message: String?, intent: Intent?, reqCode: Int) {

        val pendingIntent = PendingIntent.getActivity(context, reqCode, intent, PendingIntent.FLAG_ONE_SHOT)
        val CHANNEL_ID = "channel_name" // The id of the channel.

        val notificationBuilder = NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(android.R.drawable.ic_delete)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setContentIntent(pendingIntent)

        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val name: CharSequence = "Channel Name" // The user-visible name of the channel.
            val importance = NotificationManager.IMPORTANCE_HIGH
            val mChannel = NotificationChannel(CHANNEL_ID, name, importance)
            notificationManager.createNotificationChannel(mChannel)
        }
        notificationManager.notify(reqCode, notificationBuilder.build()) // 0 is the request code, it should be unique id
        Log.d("showNotification", "showNotification: $reqCode")
    }
}
  1. 带有alarmManager的MainActivity
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        button.setOnClickListener {

            val alarms = this.getSystemService(Context.ALARM_SERVICE) as AlarmManager

            val receiver = Receiver()
            val filter = IntentFilter("ALARM_ACTION")
            registerReceiver(receiver, filter)

            val intent = Intent("ALARM_ACTION")
            intent.putExtra("param", "My scheduled action")
            val operation = PendingIntent.getBroadcast(this, 0, intent, 0)
            val calendar = Calendar.getInstance()
            calendar.set(2020, 10, 8, 15, 0) // 8 november 2020 
                                                // 15h(3pm)
            
            alarms.set(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, 
           operation)
                                                                        
        }

    }
}
  1. 清单
<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".Receiver"/>
    </application>

我提醒即使该应用已关闭,该示例也需要工作。 您可以使用Java或kotlin(优先于Kotlin)回答。 如果可以的话,我想举一个完整的例子。

谢谢。

1 个答案:

答案 0 :(得分:0)

您的清单似乎没有用于设置闹钟的uses-permission

基本上将此行添加到清单:

<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>

我认为它是在xml上的<application>标签上方添加的

Reference

编辑 This stackoverflow answer似乎有如何添加它以正确显示