我在Android 8更高版本中特别显示通知时遇到问题,我知道它需要设置ChanelID,从我发现的所有引用中,我都必须以这种方式实现,才能设置Chanel ID,但仍然无法正常工作显示通知
在我的Gradle脚本下面
compileSdkVersion 26
buildToolsVersion "26.0.0"
useLibrary 'org.apache.http.legacy'
defaultConfig {
applicationId "com.sdept.company.blablabla"
minSdkVersion 15
targetSdkVersion 26
versionCode 9
versionName "1.9"
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
}
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.google.code.gson:gson:2.4'
compile 'org.apache.httpcomponents:httpcore:4.2.4'
compile 'org.apache.httpcomponents:httpmime:4.3'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'pl.droidsonroids.gif:android-gif-drawable:1.1.+'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.google.code.gson:gson:2.4'
compile 'com.google.zxing:core:3.2.1'
compile 'com.google.android.gms:play-services-maps:11.0.4'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
compile 'com.google.firebase:firebase-messaging:11.0.4'
testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'
这是接收者的通知类
private void showNotification(String judul,String messageBody, Bitmap image, String kode){
Intent intent = new Intent(this, Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("directto", kode);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
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(true);
if (notificationManager != null) {
notificationManager.createNotificationChannel(mChannel);
}
}
if(image != null){
NotificationCompat.Builder notificationBuilder = new
NotificationCompat.Builder(this)
.setLargeIcon(convertToBitmap())/*Notification icon
image*/
.setSmallIcon(R.drawable.logostar)
.setContentTitle(judul)
.setContentText(messageBody)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(image))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
notificationManager.notify(0 /* ID of notification */,
notificationBuilder.build());
}
}
那是我整个脚本的全部内容,只能在android 8更低的版本中使用