I need to display a local notification at a specific date even the app is closed / in background / foreground.
I succeeded to display notification when the app is in foreground or background. But if the app is closed, nothing is happening.
In this example when i click on the button, i display alarm for the 8 november 2020 at 15h. Here is my code:
- Class Receiver with 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")
}
}
- MainActivity with alarmManager
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)
}
}
}
- Manifest
<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>
I remind this example need to work even if the app is closed. You can answer in java or kotlin (in preference Kotlin). If you can i will like full exemple.
Thanks in advance.