0

I am trying to add push notifications to my android app. Currently the min sdk version is 22 and the target version is 26. Push notifications work when using an api before 26, but when running on api 26(Oreo) no notifications show up.

Research has told me that I need to use the new constructor NotificationCompat.Builder(Context, ChannelID), but I only have the depracated option available and can not seem to figure out how to use the new version. How can I get this new constructor while also allowing my app to be compatible with older api version?

private void sendNotification(String title, String messageBody, String clickAction) {


    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    String channelId = getString(R.string.default_notification_channel_id);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(title)
                    .setChannel(channelId)
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);

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

    notificationManager.notify(1 /* ID of notification */, notificationBuilder.build());


}

apply plugin: 'com.android.application'

android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
    applicationId "com.ventech.tempmonitor"
    minSdkVersion 22
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false

        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.sun.jersey:jersey-core:1.19.4'
compile 'com.android.support:design:26.+'
compile 'com.androidplot:androidplot-core:1.5.1'
compile 'com.google.code.gson:gson:2.8.1'
compile 'com.google.firebase:firebase-messaging:10.0.1'
testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'
Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
RobOhRob
  • 585
  • 7
  • 17
  • can you post your code – CodeIsLife Oct 09 '17 at 19:16
  • Did you update build.gradle with compile 'com.android.support:appcompat-v7:26.0.0' and synced? – guipivoto Oct 09 '17 at 19:24
  • build.gradle already has compile 'com.android.support:appcompat-v7:26.+' – RobOhRob Oct 09 '17 at 19:26
  • 1
    try NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, "default"); NotificationCompat.Builder (Context context) constructor was deprecated in API level 26.0.0 – CodeIsLife Oct 09 '17 at 19:27
  • and you are importing proper class, right? import android.support.v4.app.NotificationCompat – guipivoto Oct 09 '17 at 19:28
  • @CodeIsLIfe you need to reread my question. – RobOhRob Oct 09 '17 at 19:30
  • @W0rmH0le yes that is what I have imported. – RobOhRob Oct 09 '17 at 19:30
  • 1
    "build.gradle already has compile 'com.android.support:appcompat-v7:26.+'" -- first, do not use `+`. Specify a particular version (e.g., `26.1.0`). Second, if you are using that version, you have access to [the correct constructor](https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#NotificationCompat.Builder(android.content.Context,%20java.lang.String)), and it will handle older devices. Please explain what "I only have the depracated option available" and "How can I get this new constructor" mean. – CommonsWare Oct 09 '17 at 19:36
  • @CommonsWare well what I mean by that is that it does not allow a string as the 2nd argument of the constructor. It says that there is only one argument. NotificationCompat.Builder(Context) – RobOhRob Oct 09 '17 at 19:38
  • 1
    Can you post your app-level gradle? It feels like you are using an old version of.. Something. – merterpam Oct 09 '17 at 19:38
  • Somehow, you are not getting v26 of the support libraries, and definitely not 26.1.0. As merterpam suggests, post your `build.gradle` file. – CommonsWare Oct 09 '17 at 19:41
  • Possible duplicate of [NotificationCompat.Builder deprecated in Android O](https://stackoverflow.com/questions/45462666/notificationcompat-builder-deprecated-in-android-o) – Dima Kozhevin Oct 09 '17 at 19:44
  • @DimaKozhevin nooooooo – RobOhRob Oct 09 '17 at 19:50

2 Answers2

0

After upgrading my project to Android O

buildToolsVersion "26.0.1"

Lint in Android Studio is showing a deprecated warning for the follow notification builder method:

new NotificationCompat.Builder(context)

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)

complete answer is here

Attaullah
  • 3,856
  • 3
  • 48
  • 63
0

Solved it. One of you were correct.

compile 'com.android.support:appcompat-v7:26.+' was not really grabbing version 26. I switched to compile 'com.android.support:appcompat-v7:26.1.0' and it now works.

RobOhRob
  • 585
  • 7
  • 17