【问题标题】:what is notification channel id?notifications not work in api 27 [duplicate]什么是通知通道 ID?通知在 api 27 中不起作用 [重复]
【发布时间】:2020-05-31 20:03:37
【问题描述】:

我将我的项目 api 目标更新为 27,并且所有通知都被禁用。
api 26 和 27 中的通知有什么区别?

        Notification notif = new NotificationCompat.Builder(this)
                .setContentTitle(getString(R.string.app_name))
                .setContentText(message)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pIntent)
                .setSound(alarmSound)
                .setAutoCancel(true).build();
        notif.ledARGB = 0xFFff0000;
        notif.flags = Notification.FLAG_SHOW_LIGHTS;
        notif.ledOnMS = 100;
        notif.ledOffMS = 100;

        NotificationManager notificationCompatManager =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationCompatManager.notify(0, notif);

【问题讨论】:

标签: android api notifications


【解决方案1】:

Android developers website中所述:

从 Android 8.0(API 级别 26)开始,所有通知都必须是 分配给一个频道。对于每个通道,您可以设置视觉和 应用于所有通知的听觉行为 渠道。然后,用户可以更改这些设置并决定哪些 您的应用程序的通知渠道应该是侵入性的或可见的 全部。

所以您可以使用此方法在 -27 和 +27 api 中显示通知:

Java:

    void showNotification(String title, String message) {
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel("YOUR_CHANNEL_ID",
                "YOUR_CHANNEL_NAME",
                NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription("YOUR_NOTIFICATION_CHANNEL_DESCRIPTION");
        mNotificationManager.createNotificationChannel(channel);
    }
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(), "YOUR_CHANNEL_ID")
            .setSmallIcon(R.mipmap.ic_launcher) // notification icon
            .setContentTitle(title) // title for notification
            .setContentText(message)// message for notification
            .setAutoCancel(true); // clear notification after click
    Intent intent = new Intent(getApplicationContext(), ACTIVITY_NAME.class);
    PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(pi);
    mNotificationManager.notify(0, mBuilder.build());
}

科特林:

fun showNotification(title: String, message: String) {
    val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        val channel = NotificationChannel("YOUR_CHANNEL_ID",
                "YOUR_CHANNEL_NAME",
                NotificationManager.IMPORTANCE_DEFAULT)
        channel.description = "YOUR_NOTIFICATION_CHANNEL_DESCRIPTION"
        mNotificationManager.createNotificationChannel(channel)
    }
    val mBuilder = NotificationCompat.Builder(applicationContext, "YOUR_CHANNEL_ID")
            .setSmallIcon(R.mipmap.ic_launcher) // notification icon
            .setContentTitle(title) // title for notification
            .setContentText(message)// message for notification
            .setAutoCancel(true) // clear notification after click
    val intent = Intent(applicationContext, ACTIVITY_NAME::class.java)
    val pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
    mBuilder.setContentIntent(pi)
    mNotificationManager.notify(0, mBuilder.build())
}

注意:如果您想显示提醒通知,您可以将您的频道和通知的重要性设置为高,删除您的应用并安装它再次。

【讨论】:

  • 什么是频道ID?请发送更多详细信息
  • 谢谢。有没有好的库来管理通知? (检查集成)
  • 检查集成是什么意思?
  • 我正在寻找一个好的解决方案,用于在 android 中显示通知并管理 android 的不同 api 版本。
猜你喜欢
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 2021-02-17
  • 1970-01-01
  • 1970-01-01
  • 2015-08-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多