安卓 O 版本的 Heads Up 通知

3

我一直在重写我的代码以匹配 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)吗? - Appafly
@MrAppMachine 添加了 build.gradle。 - Євген Гарастович
我希望我的Medium文章能够为您提供适当的解释和代码,网址为https://medium.com/@md.noor.asad/heads-up-push-notification-from-rest-and-firebase-when-app-is-in-background-or-foreground-6c4457fb6d4b。 - Asad
2个回答

3
原来我忘记为我的通知设置声音/震动。 Heads up 仅适用于设置有声音/震动的优先级> =高的通知。

-1

请确保您已创建通知管理器。您可以在此处了解有关设置通知的信息。

希望这能帮到您!


NotificationManager 不是你创建的东西。我会像这样获取它的实例:notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); 然后使用它。 - Євген Гарастович
1
这正是我获取代码的地方。但它并不起作用。 - Євген Гарастович

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接