繁体   English   中英

如何在奥利奥或更高版本中显示提示通知

[英]How to show heads up notification in Oreo or higher

我认为在 android 8.0 中禁用提示通知,但即使在运行 android 8.0 时,android 的内置消息也具有此功能。 我知道如何通过将振动设置为通知来显示低于 8.0 设备的抬头通知。 但是不推荐在运行 Oreo 时通知振动。 所以我去互联网上搜索,有人说启用 setVibrationPattern 到通知通道会起作用。 但遗憾的是这行不通。

所以这是我的代码。

Notification  builder = new NotificationCompat.Builder(MainActivity.this)
                            .setAutoCancel(true)
                            .setWhen(System.currentTimeMillis())
                            .setSmallIcon(R.drawable.ic_android_black_24dp)
                            .setChannelId(id)
                            .setPriority(Notification.PRIORITY_HIGH)
                            .setColor(setActionColor(counter))
                            .setColorized(true)
                            .setContentIntent(pendingIntent)
                            .build();

//Notification channel code.

NotificationChannel chan2 = new NotificationChannel(SECONDARY_CHANNEL,
                            getString(R.string.noti_channel_second), NotificationManager.IMPORTANCE_HIGH);
                    chan2.setLightColor(Color.BLUE);
                    chan2.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
                    manager.createNotificationChannel(chan2);
                    manager.notify(notificationID, builder);

迟到的答案:-但我认为它值得分享。 我正在做与上面相同的事情,但在奥利奥中不会显示Heads-up notifications

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager notificationManager = 
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel notificationChannel = 
        new NotificationChannel("ch_nr", "CHART", NotificationManager.IMPORTANCE_HIGH);
    notificationChannel.setVibrationPattern(new long[]{300, 300, 300});

    if (defaultSoundUri != null) {
        AudioAttributes att = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .build();
        notificationChannel.setSound(defaultSoundUri, att);
    }

    notificationManager.createNotificationChannel(notificationChannel);
    notificationBuilder = 
        new NotificationCompat.Builder(context, notificationChannel.getId());
}

基本代码在上面。 问题是我每次都在设备上覆盖构建并且importance没有改变为 HIGH 。 直到我遇到这个文件,我才明白这一点。

重要性级别有以下限制:

您分配的重要性级别将是频道的默认值。 用户可以在 Android 设置中更改频道的重要性级别。 一旦你选择了一个重要性级别,你可以改变它的方式就会受到限制:你只能降低重要性,并且只有在用户没有明确更改它的情况下。

结论:- 在升级通知通道的重要性之前,完全卸载以前的构建或清除数据(不确定)。

你可以这样做..这适用于每个安卓版本

private NotificationManager notifManager;
public void createNotification(String aMessage) {
    final int NOTIFY_ID = 1002;

    // There are hardcoding only for show it's just strings
    String name = "my_package_channel";
    String id = "my_package_channel_1"; // The user-visible name of the channel.
    String description = "my_package_first_channel"; // The user-visible description of the channel.

    Intent intent;
    PendingIntent pendingIntent;
    NotificationCompat.Builder builder;

    if (notifManager == null) {
        notifManager =
                (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = notifManager.getNotificationChannel(id);
        if (mChannel == null) {
            mChannel = new NotificationChannel(id, name, importance);
            mChannel.setDescription(description);
            mChannel.enableVibration(true);
            mChannel.setLightColor(Color.GREEN);
            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            notifManager.createNotificationChannel(mChannel);
        }
        builder = new NotificationCompat.Builder(this, id);

        intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        builder.setContentTitle(aMessage)  // required
                .setSmallIcon(android.R.drawable.ic_popup_reminder) // required
                .setContentText(this.getString(R.string.app_name))  // required
                .setDefaults(Notification.DEFAULT_ALL)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .setTicker(aMessage)
                .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
    } else {

        builder = new NotificationCompat.Builder(this);

        intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        builder.setContentTitle(aMessage)                           // required
                .setSmallIcon(android.R.drawable.ic_popup_reminder) // required
                .setContentText(this.getString(R.string.app_name))  // required
                .setDefaults(Notification.DEFAULT_ALL)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .setTicker(aMessage)
                .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
                .setPriority(Notification.PRIORITY_HIGH);
    } // else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

    Notification notification = builder.build();
    notifManager.notify(NOTIFY_ID, notification);
}

您还需要将“设置”中的应用通知设置设置为紧急。 除非用户将应用程序设置为紧急状态,否则不会显示抬头通知。

如果您创建通知频道并执行类似操作,则提示通知有效。

NotificationChannel chan2 = new NotificationChannel(SECONDARY_CHANNEL,
                getString(R.string.noti_channel_second), NotificationManager.IMPORTANCE_HIGH);
        chan2.setLightColor(Color.BLUE);
        chan2.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        getManager().createNotificationChannel(chan2);

您设置了错误的频道 ID。

setChannelId(String channelId) 指定应该传递通知的通道。

在您的示例中,您正在为通知生成器设置一些idsetChannelId(id) ,然后创建一个您未指定的新辅助频道: chan2


这是我的工作示例:

private static final String CHANNEL_ID = "CHANNEL_NO_1";

    public void sendNotification(Context context) {

       // Create your notification channel 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            CharSequence name = "New riddle";
            String description = "Riddle of the day";
            int importance = NotificationManager.IMPORTANCE_HIGH;

            // IMPORTANT: CHANNEL_ID
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID,name, importance);
            channel.setDescription(description);


            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }


        // Get an instance of NotificationManager
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setContentTitle("Riddle of the day:")

                //IMPORTANT: CHANNEL_ID
                .setChannelId(CHANNEL_ID)

        // It won't show "Heads Up" unless it plays a sound     
        if (Build.VERSION.SDK_INT >= 21) mBuilder.setVibrate(new long[0]);


        // Gets an instance of the NotificationManager service//
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);


        mNotificationManager.notify(001, mBuilder.build());
    }

还有一件事,请确保在进行这些更改后重新安装您的应用程序。

在某些手机中,有一个状态栏和通知设置,如果通知的顶部预览设置为no preview ,那么它将不允许任何应用程序显示抬头,如果您已经将其设置为横幅将解决您的问题正确设置您的频道和通知。

我在 Android 9 操作系统 Vivo y1902 手机上遇到了同样的问题,在进行上述更改后已解决。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM