NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID) not working on Oreo Firebase notification

痴心易碎 提交于 2019-12-19 07:25:08

问题


I am trying to show notification using Firebase in Oreo version so it's not showing when I get Solution
NotificationCompat.Builder(this, CHANNEL_ID) but it's showing me like this

and my build.gradle file is

    apply plugin: 'com.android.application'

    dependencies {
    compile project(':library')
    compile project(':camerafragment')
    compile 'com.google.android.gms:play-services:11.0.0'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.mcxiaoke.volley:library:1.0.17'
    compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
    compile 'com.android.support:cardview-v7:26.0.0-alpha1'
    compile 'com.google.firebase:firebase-messaging:11.0.0'
    compile 'com.google.android.gms:play-services-maps:11.0.0'
    compile 'com.facebook.android:facebook-android-sdk:[4,5)'
    compile 'com.android.support:design:26.0.0-alpha1'
    compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
    compile 'com.jakewharton:butterknife:7.0.1'
    compile 'com.google.android.gms:play-services-auth:11.0.0'
    compile 'net.gotev:uploadservice:2.1'
    compile 'com.google.firebase:firebase-auth:11.0.0'
    compile 'com.google.code.gson:gson:2.8.0'
    compile 'com.android.support:support-v4:26.0.0-alpha1'
    }

    android {

    compileSdkVersion 27
    buildToolsVersion "27.0.0"
    dexOptions {
        javaMaxHeapSize "4g"
    }
    defaultConfig {
        applicationId "com.trashmap"
        minSdkVersion 17
        targetSdkVersion 27

        // Enabling multidex support.
        multiDexEnabled true
        versionCode 17
        versionName "1.16"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
           // shrinkResources true//new add to reduce size
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

    }



      sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            res.srcDirs = ['res']
        }
    }
}

apply plugin: 'com.google.gms.google-services'

回答1:


NotificationCompat.Builder (Context context)

This constructor was deprecated in API level 26.1.0.

use NotificationCompat.Builder(Context, String) instead. All posted Notifications must specify a NotificationChannel Id.

And you have defined compile 'com.android.support:support-v4:26.0.0-alpha1' So you have to change your version number of support library.




回答2:


It is mentioned in the documentation that the builder method NotificationCompat.Builder(Context context) has been deprecated. And we have to use the constructor which has the channelId parameter:

NotificationCompat.Builder(Context context, String channelId) https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html

This constructor was deprecated in API level 26.0.0-beta1. use NotificationCompat.Builder(Context, String) instead. All posted Notifications must specify a NotificationChannel Id. https://developer.android.com/reference/android/app/Notification.Builder.html

This constructor was deprecated in API level 26. use Notification.Builder(Context, String) instead. All posted Notifications must specify a NotificationChannel Id. If you want to reuse the builder setters, you can create the builder with the channelId, and pass that builder to a helper method and set your preferred settings in that method.

Try this one hope so it will be working...

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getContext(), "CHANNEL_ID");

        notificationBuilder.setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.ic_launcher)
                .setTicker("Dilip21")
                .setContentTitle("Default notification")
                .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
                .setContentInfo("Info");

NotificationManager notificationManager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notificationBuilder.build());


来源:https://stackoverflow.com/questions/47567676/notificationcompat-buildergetapplicationcontext-channel-id-not-working-on-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!