[英]notification channel is not sending notifications
private void sendNotif(String title , String text , int id) {
// Create an explicit intent for an Activity in your app
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "mychannel")
.setSmallIcon(android.R.drawable.screen_background_light)
.setContentTitle(title)
.setContentText(text)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(text))
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setChannelId("mychannel")
.setAutoCancel(true)
.setOngoing(false);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(id , builder.build());
}
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "notif channel";
String description = "this is the main notif channel";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel("mychannel", name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
I have two simple methods one to send notifications and other to a create a notification channel Everything seems alright to me, but no notifications are being sent also i would like to mention that the same channel is created twice (i took a look into the app's notifaction settings), although createNotificationChanne()
is called once also the docs mentions that :我有两种简单的方法,一种是发送通知,另一种是创建通知频道 对我来说一切都很好,但没有发送通知我想提一下,同一个频道创建了两次(我查看了应用程序的通知设置),虽然
createNotificationChanne()
被调用一次,文档也提到:
It's safe to call this repeatedly because creating an existing notification channel performs no operation.
重复调用它是安全的,因为创建现有的通知通道不执行任何操作。
but it did obviously但它显然做到了
i am using android 10 and here's my build.gradle file :我正在使用 android 10,这是我的 build.gradle 文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.1"
defaultConfig {
applicationId ""
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
implementation 'com.google.android.material:material:1.2.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
thanks in advance提前致谢
I've tried your code, It works pretty well.我试过你的代码,效果很好。 I think you might have forgotten to call the
createNotificationChannel()
before creating the notification.我认为您可能忘记在创建通知之前调用
createNotificationChannel()
。
private void sendNotif(String title , String text , int id) {
createNotificationChannel()
// create notification code goes here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.