Android通知:抬头和锁定屏幕通知

时间:2019-01-02 04:34:00

标签: android android-notifications lockscreen heads-up-notifications

我正在使用以下代码来显示提示和锁定屏幕通知,但无法正常工作。谁能找出可能出问题的地方?

仅当我打开手机设置的抬头并锁定屏幕的通知时,此功能才有效。我正在小米Redmi 5A设备上测试此代码。

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID = context.getString(R.string.app_name);
        String NOTIFICATION_CHANNEL_NAME = context.getString(R.string.app_name);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);

            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
                    .build();

            // Configure the notification channel.
            notificationChannel.setName(notificationTitle);
            notificationChannel.setDescription(notificationBody);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setVibrationPattern(new long[]{0, 1000});
            notificationChannel.enableVibration(true);
            notificationChannel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);
            notificationChannel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI, audioAttributes);
            notificationManager.createNotificationChannel(notificationChannel);
        }

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);

        Intent notificationIntent = new Intent(context, SingleMessageThreadActivity.class);

        notificationIntent.putExtra("phoneNumber", PHIONE_NUMBER);

        PendingIntent pendingIntent =
                TaskStackBuilder.create(context)
                        // add all of DetailsActivity's parents to the stack,
                        // followed by DetailsActivity itself
                        .addNextIntentWithParentStack(notificationIntent)
                        .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        notificationBuilder.setAutoCancel(true)
                .setVibrate(new long[]{0, 1000})
                .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.launcher_icon)
                .setContentIntent(pendingIntent)
                .setContentTitle(notificationTitle)
                .setContentText(notificationBody);

        notificationManager.notify(/*notification id*/1, notificationBuilder.build());

0 个答案:

没有答案
相关问题