android - 调用需要API 26(最小为18):android.app.NotificationChannel

标签 android gradle android-notifications

我想在android API <26中发送通知,但遇到此错误。

我想我的Gradle文件也有问题。

我尝试添加条件android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O的if语句
但是它不能在低于26的API上运行。我们需要它才能在API 18上运行。

导入的软件包:

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import static android.preference.PreferenceManager.getDefaultSharedPreferences;


通知代码:
 //Create notification manager
                        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

                        //Create channel in which to send push notifications
                        String CHANNEL_ID = "my_channel_01";
                        CharSequence name = "my_channel";
                        String Description = "This is my channel";
                        int importance = NotificationManager.IMPORTANCE_HIGH;
                        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
                        mChannel.setDescription(Description);
                        mChannel.enableLights(true);
                        mChannel.setLightColor(Color.RED);
                        mChannel.enableVibration(true);
                        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                        mChannel.setShowBadge(false);
                        notificationManager.createNotificationChannel(mChannel);

                        //Send push notification
                        Notification notify = new Notification.Builder(getApplicationContext())
                                .setContentTitle("Listener available on pSquared!")
                                .setContentText("You can now talk about your day in a pSquared chatbox with a Listener")
                                .setSmallIcon(R.drawable.psquared_logo).setChannelId(CHANNEL_ID).build();

                        notify.flags |= Notification.FLAG_AUTO_CANCEL;
                        notificationManager.notify(0, notify);

Gradle文件:
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.psquared"
        minSdkVersion 18
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.google.firebase:firebase-database:16.1.0'
    implementation 'com.google.firebase:firebase-auth:16.2.1'

    implementation 'com.firebaseui:firebase-ui-auth:4.1.0'
    implementation 'com.firebaseui:firebase-ui-database:4.3.2'
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0'


    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.firebaseui:firebase-ui:1.2.0'
    implementation 'com.firebase:firebase-client-android:2.5.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    //compile 'com.android.support:design:25.0.1'
}


该行:
    implementation 'com.android.support:appcompat-v7:28.0.0'

带有红色下划线的错误:

All com.android.support libraries must use the exact same version specification (mixing versions can lead to runtime crashes). Found versions 28.0.0, 25.1.1. Examples Include: com.android.support:animated-vector-drawable:28.0.0 and com.android.support:palette-v7:25.1.1 less... (Ctrl+F1)



我希望通知能够在Android API 18及更高版本上运行

最佳答案

您需要在Api级别> = 26上使用NotificationChannel并使用NotificationCompat.Builder构造通知。因此,将您的代码更改为如下所示:

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

//Create channel in which to send push notifications
String CHANNEL_ID = "my_channel_01";

// only use NotificationChannel when Api Level >= 26
if(Build.VERSION.SDK_INT >= 26) {
    CharSequence name = "my_channel";
    String Description = "This is my channel";
    int importance = NotificationManager.IMPORTANCE_HIGH;
    NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
    mChannel.setDescription(Description);
    mChannel.enableLights(true);
    mChannel.setLightColor(Color.RED);
    mChannel.enableVibration(true);
    mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
    mChannel.setShowBadge(false);
    notificationManager.createNotificationChannel(mChannel);
}

//Send push notification
Notification notify = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
        .setContentTitle("Listener available on pSquared!")
        .setContentText("You can now talk about your day in a pSquared chatbox with a Listener")
        .setSmallIcon(R.drawable.psquared_logo)
        .build();

notify.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notify);

关于android - 调用需要API 26(最小为18):android.app.NotificationChannel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55977225/

相关文章:

eclipse - 如何为新的 eclipse (neon) java 项目初始化 git

java - 服务停止时如何删除通知?

android - 在通知栏上显示 LevelListDrawable

android - Android L 预览版/API 20 中通知类别是如何分配的

android - 将默认文本设置为 android 中的微调器 View

java - 如何在 Android 应用程序的多次调用中保存序列号状态?

android - Parcelable 接口(interface)保留对象引用? &Parcelable.CREATOR 是不是叫了?

Gradle:在 taskGraph.whenReady 中设置变量

git - 将工件添加到 gitlab-ci.yml 中的存储库

android - Firestore 安全规则 : PERMISSION DENIED when Batching