NotificationCompat.Builder只有不推荐使用的构造函数
我试图将推送通知添加到我的android应用程序。目前,min sdk版本是22,目标版本是26.推送通知在26之前使用api时工作,但当在api 26(奥利奥)上运行时,没有通知出现。研究告诉我,我需要使用新的构造函数NotificationCompat.Builder(Context, ChannelID)
,但我只有depracated选项可用,似乎无法弄清楚如何使用新版本。我怎样才能得到这个新的构造函数,同时还允许我的应用程序与旧的api版本兼容?NotificationCompat.Builder只有不推荐使用的构造函数
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'
为了使你的代码有26个或以上兼容的,你可以在你的SDK版本
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this);
if (Build.VERSION.SDK_INT >= 26) {
notificationBuilder.setChannelId("default");
}
这部分帮助....但我仍然需要能够使用新的构造函数。它是让我使用的唯一一个是NotificationCompat.Builder(上下文) – RobOhRob
我的项目升级到的AndroidØ
后buildToolsVersion "26.0.1"
皮棉Android Studio中被示出为后续通知生成器方法的弃用警告:
new NotificationCompat.Builder(context)
据该建筑工地方法NotificationCompat.Builder(上下文语境)已弃用的文档中提到的。我们不得不使用它具有的channelID参数构造函数:
NotificationCompat.Builder(Context context, String channelId)
完整的答案是here
我的问题是否有你实际阅读的任何部分? – RobOhRob
你可以发布你的代码 – CodeIsLife
你有更新build.gradle与编译'com.android.support:appcompat-v7:26.0.0'和同步? – W0rmH0le
build.gradle已经编译'com.android.support:appcompat-v7:26.+' – RobOhRob