web-dev-qa-db-ja.com

通知チャネルIDとは何ですか?通知はAPI 27で機能しません

プロジェクトの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);
24

このメソッドを使用して、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(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_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 [〜#〜] =アプリを削除して、再度インストールします。

43
Pouya Heydari