我试图将推送通知添加到我的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'
你可以发布你的代码 – CodeIsLife
你有更新build.gradle与编译'com.android.support:appcompat-v7:26.0.0'和同步? – W0rmH0le
build.gradle已经编译'com.android.support:appcompat-v7:26.+' – RobOhRob