JobIntentService(AppWidgetProvider)添加通知

时间:2018-12-12 18:35:07

标签: android widget

我在android 8.0+上。我的 AppWidgetProvider 中有一个 JobIntentService 。 现在,我想添加一个通知,但看起来好像从未添加。为什么?

我的代码:

override fun onHandleWork(intent: Intent) {
    ...

    Utils.addNotification(App.getContext(), textnotiIdo, textnotiText)

...

实用程序:

fun addNotification(c: Context, title: String, text: String) {
    val remoteViews = RemoteViews(c.packageName, R.layout.notification)
    remoteViews.setTextViewText(R.id.title, HtmlCompat.fromHtml(title))
    remoteViews.setTextViewText(R.id.text, HtmlCompat.fromHtml(text))

    val uniqid = System.currentTimeMillis().toInt() and 0xffffff
    val channelId = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) createNotificationChannel() else ""
    val contentIntent = PendingIntent.getActivity(c, uniqid, Intent(), PendingIntent.FLAG_CANCEL_CURRENT)

    val builder = NotificationCompat.Builder(c, channelId)
            .setSmallIcon(R.drawable.icon)
            //.setContent(remoteViews)
            .setContentIntent(contentIntent)
            .setTicker("a")
            .setContentText("a")
            .setSubText("a")

    //val notificationIntent = Intent(c, MainActivity::class.java)

    val manager = c.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    manager.notify(mNotifyID++, builder.build())

    Logger.E("addnoti", title, text)
}

@RequiresApi(Build.VERSION_CODES.O)
private fun createNotificationChannel(): String {
    val channelId = "my_service"
    val channelName = "My Background Service"
    val chan = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_NONE)
    chan.lightColor = Color.BLUE
    chan.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
    val service = App.getContext().getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    service.createNotificationChannel(chan)
    return channelId
}

这里有什么问题?为什么我没有收到任何通知? (我可以在LogCat中看到“ addnoti”字符串。)

0 个答案:

没有答案