【问题标题】:Android O heads up notificationsAndroid O 提醒通知
【发布时间】:2017-08-31 14:08:39
【问题描述】:

我一直在重写我的代码以匹配 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;
    }

来自我的 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
}

【问题讨论】:

  • 你编译 sdk 26 吗?我可以看看你的 gradle.build(app)
  • @MrAppMachine 添加了 build.gradle
  • 我希望我的中等帖子能帮助您在medium.com/@md.noor.asad/…进行正确的解释和代码

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    确保您创建了通知管理器。您可以了解如何设置通知here

    希望这会有所帮助!

    【讨论】:

    • NotificationManager 不是您创建的。我得到它的实例是这样的:notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);并使用它。
    • 这正是我获取代码的地方。它不起作用
    猜你喜欢
    • 1970-01-01
    • 2021-03-23
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多