什么是通知通道ID?通知在api 27中不起作用

时间:2017-11-21 09:12:21

标签: android api notifications

我将项目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);

1 个答案:

答案 0 :(得分:31)

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

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_DISCRIPTION");
        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(content)// 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());
}

Kotlin:

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_DISCRIPTION"
        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())
}

注意:如果您想要显示抬头通知,可以将频道和通知的重要性设置为HIGH, AND 删除您的应用并安装试。