gpt4 book ai didi

java - Android 8.0 Oreo 上不显示通知

转载 作者:行者123 更新时间:2023-12-02 12:02:27 25 4
gpt4 key购买 nike

我读过有关 Android 8.0 Oreo 上未出现通知的其他 Stackoverflow 文章,但该代码仍然无法在 Android Android api 26 上运行。

代码如下:

    public void notification()
{

//Create Notification Channel
String channelId = "";
String name = "volume";
int id = 0;
int importance = NotificationManager.IMPORTANCE_HIGH;

NotificationChannel mChannel = new NotificationChannel(channelId, name,
importance);
mChannel.setShowBadge(true);

//Create Notification Manager
Intent resultIntent = new Intent(this, MainActivity.class);

PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_CANCEL_CURRENT
);

NotificationCompat.Builder mBuilder = new
NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setContentIntent(resultPendingIntent)
;

// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyMgr.createNotificationChannel(mChannel);

// Builds the notification and issues it.
mNotifyMgr.notify(id, mBuilder.build());

}

更新1:

我已经更改了 String channelId = "name",但它仍然不起作用。我什至尝试了下面的另一组代码,但仍然不起作用:

        NotificationManager notificationManager = (NotificationManager) 
getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new
NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications",
NotificationManager.IMPORTANCE_DEFAULT);

// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500,
1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}

NotificationCompat.Builder builder = new
NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setVibrate(new long[]{0, 100, 100, 100, 100, 100})

.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Content Title")
.setContentText("Content Text");

notificationManager.notify(NOTIFICATION_ID, builder.build());

更新2:

我的build.gradle(项目:测试)如下:

// Top-level build file where you can add configuration options common to 
all sub-projects/modules.

buildscript {

repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'


// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
google()
jcenter()
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

我的build.gradle(模块:app)如下:

apply plugin: 'com.android.application'

android {
compileSdkVersion 26
defaultConfig {
applicationId "com.example.a0064274652969.test"
minSdkVersion 26
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 {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

最佳答案

所以我复制并粘贴了您的原始代码,它运行良好(在模拟器中),这意味着 channel ID 的空字符串很好,通知 ID 的值为零。

您的错误在其他地方,可能在您设备的 android 设置中)。尝试发布你的 build.gradle 吗?

<小时/>

If you target Android O and post a notification without specifying a valid notifications channel, the notification fails to post and the system logs an error.

对于您的新代码,您不再设置 contentIntent...另外,请务必收到“提醒通知”,您需要将重要性设置为“HIGH”或“MAX”。

关于java - Android 8.0 Oreo 上不显示通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47149696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com