[英]How to set multiple notifications in android using Work Manager?
I have implemented background notifications using this article: Click Here To View The Article我已经使用本文实现了背景通知: 单击此处查看文章
It sets the notification for future time.它将通知设置为将来的时间。 But it sets only one notification i want to set multiple notifications.
但是它只设置了一个通知,而我想设置多个通知。 And also sometimes it doesn't show the notification on the time that i have set and it shows when i open the app.
有时它也不会在我设置的时间显示通知,而是在我打开应用程序时显示。
Here is my Activity code:这是我的活动代码:
private void saveData() {
long currentTime = System.currentTimeMillis();
//currentTime is the system current time when user sets the notification.
if (closingMilliSeconds > currentTime) {
//closingMilliSeconds is the future time when the notification should show up.
Data data = new Data.Builder().putInt(NOTIFICATION_ID, 0).build();
long delay = closingMilliSeconds - currentTime;
//delay is the time difference between current time and the future time when the notification should show up.
scheduleNotification(delay, data);
}
}
private void scheduleNotification(long delay, Data data) {
OneTimeWorkRequest notificationWork = new OneTimeWorkRequest.Builder(NotifyWork.class)
.setInitialDelay(delay, TimeUnit.MILLISECONDS)
.setInputData(data)
.build();
WorkManager instanceWorkManager = WorkManager.getInstance(this);
instanceWorkManager.beginUniqueWork(NOTIFICATION_WORK, REPLACE, notificationWork).enqueue();
}
NotifyWork Class通知工作类别
package com.example.wongsigntenders.notificationHandler.work
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.NotificationManager.IMPORTANCE_HIGH
import android.app.PendingIntent.getActivity
import android.content.Context
import android.content.Context.NOTIFICATION_SERVICE
import android.content.Intent
import android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
import android.graphics.Color.RED
import android.media.AudioAttributes
import android.media.AudioAttributes.CONTENT_TYPE_SONIFICATION
import android.media.AudioAttributes.USAGE_NOTIFICATION_RINGTONE
import android.media.RingtoneManager.TYPE_NOTIFICATION
import android.media.RingtoneManager.getDefaultUri
import android.os.Build.VERSION.SDK_INT
import android.os.Build.VERSION_CODES.O
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationCompat.DEFAULT_ALL
import androidx.core.app.NotificationCompat.PRIORITY_MAX
import androidx.work.ListenableWorker.Result.success
import androidx.work.Worker
import androidx.work.WorkerParameters
import com.example.wongsigntenders.R
import com.example.wongsigntenders.activities.MainActivity
import com.example.wongsigntenders.notificationHandler.extension.vectorToBitmap
class NotifyWork(context: Context, params: WorkerParameters) : Worker(context, params) {
override fun doWork(): Result {
val id: Int = inputData.getInt(NOTIFICATION_ID, 0)
sendNotification(id)
return success()
}
private fun sendNotification(id: Int) {
val intent = Intent(applicationContext, MainActivity::class.java)
intent.flags = FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_CLEAR_TASK
intent.putExtra(NOTIFICATION_ID, id)
val notificationManager =
applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
val bitmap = applicationContext.vectorToBitmap(R.drawable.ic_info_24)
val titleNotification = "Wongsign Tenders"
val subtitleNotification = "My Wongsign Tender Notification...!!!"
val pendingIntent = getActivity(applicationContext, 0, intent, 0)
val notification = NotificationCompat.Builder(applicationContext, NOTIFICATION_CHANNEL)
.setLargeIcon(bitmap).setSmallIcon(R.drawable.ic_info_24)
.setContentTitle(titleNotification).setContentText(subtitleNotification)
.setDefaults(DEFAULT_ALL).setContentIntent(pendingIntent).setAutoCancel(true)
notification.priority = PRIORITY_MAX
if (SDK_INT >= O) {
notification.setChannelId(NOTIFICATION_CHANNEL)
val ringtoneManager = getDefaultUri(TYPE_NOTIFICATION)
val audioAttributes = AudioAttributes.Builder().setUsage(USAGE_NOTIFICATION_RINGTONE)
.setContentType(CONTENT_TYPE_SONIFICATION).build()
val channel =
NotificationChannel(NOTIFICATION_CHANNEL, NOTIFICATION_NAME, IMPORTANCE_HIGH)
channel.enableLights(true)
channel.lightColor = RED
channel.enableVibration(true)
channel.vibrationPattern = longArrayOf(400, 500)
channel.setSound(ringtoneManager, audioAttributes)
notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(id, notification.build())
}
companion object {
const val NOTIFICATION_ID = "appName_notification_id"
const val NOTIFICATION_NAME = "appName"
const val NOTIFICATION_CHANNEL = "appName_channel_01"
const val NOTIFICATION_WORK = "appName_notification_work"
}
}
VectorToBitmap Converter VectorToBitmap转换器
package com.example.wongsigntenders.notificationHandler.extension
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Bitmap.createBitmap
import android.graphics.Canvas
import androidx.core.content.ContextCompat.getDrawable
fun Context.vectorToBitmap(drawableId: Int): Bitmap? {
val drawable = getDrawable(this, drawableId) ?: return null
val bitmap = createBitmap(
drawable.intrinsicWidth, drawable.intrinsicHeight, Bitmap.Config.ARGB_8888
) ?: return null
val canvas = Canvas(bitmap)
drawable.setBounds(0, 0, canvas.width, canvas.height)
drawable.draw(canvas)
return bitmap
}
For further details you can have a look on This Article有关更多详细信息,请查看本文。
Thanks in advance.提前致谢。 Please let me know if anyone can help.
请让我知道是否有人可以提供帮助。
I have implemented background notifications using this article: Click Here To View The Article我已经使用本文实现了背景通知: 单击此处查看文章
It sets the notification for future time.它将通知设置为将来的时间。 But it sets only one notification i want to set multiple notifications.
但是它只设置了一个通知,而我想设置多个通知。 And also sometimes it doesn't show the notification on the time that i have set and it shows when i open the app.
有时它也不会在我设置的时间显示通知,而是在我打开应用程序时显示。
Here is my Activity code:这是我的活动代码:
private void saveData() {
long currentTime = System.currentTimeMillis();
//currentTime is the system current time when user sets the notification.
if (closingMilliSeconds > currentTime) {
//closingMilliSeconds is the future time when the notification should show up.
Data data = new Data.Builder().putInt(NOTIFICATION_ID, 0).build();
long delay = closingMilliSeconds - currentTime;
//delay is the time difference between current time and the future time when the notification should show up.
scheduleNotification(delay, data);
}
}
private void scheduleNotification(long delay, Data data) {
OneTimeWorkRequest notificationWork = new OneTimeWorkRequest.Builder(NotifyWork.class)
.setInitialDelay(delay, TimeUnit.MILLISECONDS)
.setInputData(data)
.build();
WorkManager instanceWorkManager = WorkManager.getInstance(this);
instanceWorkManager.beginUniqueWork(NOTIFICATION_WORK, REPLACE, notificationWork).enqueue();
}
NotifyWork Class通知工作类别
package com.example.wongsigntenders.notificationHandler.work
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.NotificationManager.IMPORTANCE_HIGH
import android.app.PendingIntent.getActivity
import android.content.Context
import android.content.Context.NOTIFICATION_SERVICE
import android.content.Intent
import android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
import android.graphics.Color.RED
import android.media.AudioAttributes
import android.media.AudioAttributes.CONTENT_TYPE_SONIFICATION
import android.media.AudioAttributes.USAGE_NOTIFICATION_RINGTONE
import android.media.RingtoneManager.TYPE_NOTIFICATION
import android.media.RingtoneManager.getDefaultUri
import android.os.Build.VERSION.SDK_INT
import android.os.Build.VERSION_CODES.O
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationCompat.DEFAULT_ALL
import androidx.core.app.NotificationCompat.PRIORITY_MAX
import androidx.work.ListenableWorker.Result.success
import androidx.work.Worker
import androidx.work.WorkerParameters
import com.example.wongsigntenders.R
import com.example.wongsigntenders.activities.MainActivity
import com.example.wongsigntenders.notificationHandler.extension.vectorToBitmap
class NotifyWork(context: Context, params: WorkerParameters) : Worker(context, params) {
override fun doWork(): Result {
val id: Int = inputData.getInt(NOTIFICATION_ID, 0)
sendNotification(id)
return success()
}
private fun sendNotification(id: Int) {
val intent = Intent(applicationContext, MainActivity::class.java)
intent.flags = FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_CLEAR_TASK
intent.putExtra(NOTIFICATION_ID, id)
val notificationManager =
applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
val bitmap = applicationContext.vectorToBitmap(R.drawable.ic_info_24)
val titleNotification = "Wongsign Tenders"
val subtitleNotification = "My Wongsign Tender Notification...!!!"
val pendingIntent = getActivity(applicationContext, 0, intent, 0)
val notification = NotificationCompat.Builder(applicationContext, NOTIFICATION_CHANNEL)
.setLargeIcon(bitmap).setSmallIcon(R.drawable.ic_info_24)
.setContentTitle(titleNotification).setContentText(subtitleNotification)
.setDefaults(DEFAULT_ALL).setContentIntent(pendingIntent).setAutoCancel(true)
notification.priority = PRIORITY_MAX
if (SDK_INT >= O) {
notification.setChannelId(NOTIFICATION_CHANNEL)
val ringtoneManager = getDefaultUri(TYPE_NOTIFICATION)
val audioAttributes = AudioAttributes.Builder().setUsage(USAGE_NOTIFICATION_RINGTONE)
.setContentType(CONTENT_TYPE_SONIFICATION).build()
val channel =
NotificationChannel(NOTIFICATION_CHANNEL, NOTIFICATION_NAME, IMPORTANCE_HIGH)
channel.enableLights(true)
channel.lightColor = RED
channel.enableVibration(true)
channel.vibrationPattern = longArrayOf(400, 500)
channel.setSound(ringtoneManager, audioAttributes)
notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(id, notification.build())
}
companion object {
const val NOTIFICATION_ID = "appName_notification_id"
const val NOTIFICATION_NAME = "appName"
const val NOTIFICATION_CHANNEL = "appName_channel_01"
const val NOTIFICATION_WORK = "appName_notification_work"
}
}
VectorToBitmap Converter VectorToBitmap转换器
package com.example.wongsigntenders.notificationHandler.extension
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Bitmap.createBitmap
import android.graphics.Canvas
import androidx.core.content.ContextCompat.getDrawable
fun Context.vectorToBitmap(drawableId: Int): Bitmap? {
val drawable = getDrawable(this, drawableId) ?: return null
val bitmap = createBitmap(
drawable.intrinsicWidth, drawable.intrinsicHeight, Bitmap.Config.ARGB_8888
) ?: return null
val canvas = Canvas(bitmap)
drawable.setBounds(0, 0, canvas.width, canvas.height)
drawable.draw(canvas)
return bitmap
}
For further details you can have a look on This Article有关更多详细信息,请查看本文。
Thanks in advance.提前致谢。 Please let me know if anyone can help.
请让我知道是否有人可以提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.