java - NotificationCompat v7 不适用于 Oreo 及更高版本的操作系统

标签 java android android-layout notifications android-notifications

我在 android 8,9 上使用 NotificationCompat v7 时遇到问题。 在所有 Android 版本上显示通知的最佳方式是什么

调试日志

android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=0 contentView=null vibrate=null sound=null tick defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE)

MinSDK=18,TargetSDK=28,Android Gradle 插件版本=3.4.1,gradle 版本=5.1.1

如何解决这个问题有什么好的解决办法吗?

我的 gradle 文件

dependencies {
    implementation "com.android.support:support-core-utils:28.0.0"
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:2.0.0-alpha5'
    testImplementation 'junit:junit:4.13-beta-2'
   'com.android.support.test:runner:1.0.2'
    implementation "com.android.support:support-core-utils:28.0.0"
    implementation 'com.google.code.gson:gson:2.8.5'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.android.support:support-compat:28.0.0'
}





 @Override
        public void onCreate() {
            super.onCreate();
            wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            hotspotControl = HotspotControl.getInstance(getApplicationContext());
            m_notificationStopActionReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    if (null != intent && WIFI_AP_ACTION_STOP.equals(intent.getAction()))
                        disableHotspotAndStop();
                }
            };
            registerReceiver(m_notificationStopActionReceiver, new IntentFilter(WIFI_AP_ACTION_STOP));
            //Start a foreground with message saying 'Initiating Hotspot'. Message is later updated using SHARE_SERVICE_NOTIFICATION_ID
            startForeground(SHARE_SERVICE_NOTIFICATION_ID, getNotification(getString(R.string.p2p_sender_service_init_notification_header), false));
            hotspotCheckHandler = new HotspotChecker(this);
        }

        protected android.support.v4.app.NotificationCompat.Action getStopAction() {
            Intent intent = new Intent(WIFI_AP_ACTION_STOP);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            return new android.support.v4.app.NotificationCompat.Action.Builder(android.R.drawable.ic_menu_close_clear_cancel, "Stop", pendingIntent).build();
        }

最佳答案

您需要使用通知 channel 在 Android 8.0+ 设备上显示通知。

您可以使用以下代码通过 getShareThemNotification(...) 方法生成通知。

    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

        NotificationChannel notificationChannel = new NotificationChannel("channel_id",
                "Channel Name", NotificationManager.IMPORTANCE_HIGH);
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.GREEN);
        notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        notificationManager.createNotificationChannel(notificationChannel);

        Notification.Builder builder = new Notification.Builder(context, "channel_id");
        notification = builder.setContentTitle({"Title"})
                .setContentText({"Message"})
                .setAutoCancel(true)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentIntent(pendingIntent).build();
    } else {
        Notification.Builder builder = new Notification.Builder(context);
        notification = builder.setContentTitle({"Title"})
                .setContentText({"Message"})
                .setAutoCancel(true) 
                .setSmallIcon(R.drawable.ic_notification)
                .setContentIntent(pendingIntent).build();
    }
    notificationManager.notify(id, notification);

关于java - NotificationCompat v7 不适用于 Oreo 及更高版本的操作系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56733848/

相关文章:

java - 如何从其文件中获取证书类型?

android - 使用OpenCV在Camera View中进行Android实时DFT

android - 实现汉堡菜单后无法在 GridView 布局中滚动

java - Android:语言更改后刷新当前 fragment

java - hibernate 加载不加载整棵树

android - 为什么Eclipse无法编译Google示例?

java - 重用 NDK 代码需要安装 NDK 吗?

android - 找不到以下类 : android. support.v7.internal.app.WindowDecorActionBar

Android-dispatchTouchEvent 没有捕捉到 ACTION_CANCEL

Java 集合 HashTable 和 HashMap