Android O 抬头通知

标签 android android-8.0-oreo heads-up-notifications

我一直在重写我的代码以匹配 Android O 通知更改,但提示通知已停止工作。我的目标 SDK 版本 26 及以下是我的代码。

请帮助我让提示通知正常工作。感谢您的帮助。

    public void createNotificationChannel() {
NotificationChannel messagesChannel = new NotificationChannel(MESSAGES_CHANNEL, context.getString(R.string.channel_messages),
            NotificationManagerCompat.IMPORTANCE_HIGH);
    messagesChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION),
            new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).build());
    messagesChannel.setVibrationPattern(new long[]{0, 1000, 1000, 1000, 1000});
    messagesChannel.setLightColor(Color.WHITE);
    messagesChannel.setShowBadge(true);
    notificationManager.createNotificationChannel(messagesChannel);
}
    public void showMessageNotification(final String conversationId, final String message) {
        Intent contentIntent = ConversationDetailsActivity.getLaunchIntent(this, conversationId);
        contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(ConversationDetailsActivity.class);
        stackBuilder.addNextIntent(contentIntent);

        PendingIntent contentPendingIntent = stackBuilder.getPendingIntent(conversationId.hashCode(), FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

        Notification notification = buildNotification(R.drawable.ic_message_24dp, message, contentPendingIntent);
        notificationManager.notify(conversationId.hashCode(), notification);
    }
    private Notification buildNotification(final int iconResId, final String message, final PendingIntent contentPendingIntent) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, MESSAGES_CHANNEL);
        Notification notification = builder
                .setSmallIcon(iconResId)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setContentTitle(getString(R.string.notification_title))
                .setContentText(message)
                .setContentIntent(contentPendingIntent)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .build();
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        return notification;
    }

来 self 的 build.gradle

compileSdkVersion 26
buildToolsVersion '26.0.1'
defaultConfig {
    applicationId "com.xxxxxxxx"
    minSdkVersion 17
    targetSdkVersion 26
    versionCode 21
    versionName '1.13'
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    multiDexEnabled true
}

最佳答案

原来我忘记为我的通知设置声音/振动。注意仅适用于设置了声音/振动的优先级 >= 高的通知。

关于Android O 抬头通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45983531/

相关文章:

java - 如何在android中重命名文件

java - Android Studio - 运行 MainActivity 时出错 : Unknown run configuration type AndroidRunConfigurationType

android - phonegap/cordova filewriter 和 reader 不工作

android - 始终在后台以 root 身份在 android 上运行服务

android - 不启动新 Activity 的通知操作?

java - 有没有办法暂时隐藏或禁用平视通知?

Android 2.3.6 + Phonegap + localStorage

android - 如何让通知图标在 Android 8.0.0 上正确显示?

android - 无法在模拟器中运行的应用程序中运行谷歌地图

android - 提醒通知未按预期工作