简体   繁体   English

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

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

I think heads up notification disable in android 8.0 but android's built-in message have this feature even on running android 8.0 .我认为在 android 8.0 中禁用提示通知,但即使在运行 android 8.0 时,android 的内置消息也具有此功能。 I know how to show heads up notification lower than 8.0 device by setting vibration to notification.我知道如何通过将振动设置为通知来显示低于 8.0 设备的抬头通知。 But vibration on notification running Oreo is deprecated.但是不推荐在运行 Oreo 时通知振动。 So i go and search in internet and someone says enabling setVibrationPattern to notification channel will works.所以我去互联网上搜索,有人说启用 setVibrationPattern 到通知通道会起作用。 But saddly this not works .但遗憾的是这行不通。

So here is my code .所以这是我的代码。

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);

Late Answer :- But i think its worth sharing.迟到的答案:-但我认为它值得分享。 I was doing the same thing as above but Heads-up notifications won't shows in Oreo.我正在做与上面相同的事情,但在奥利奥中不会显示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());
}

Essential code is above .基本代码在上面。 The problem was i was overriding build each time on device and the importance did not get change to HIGH .问题是我每次都在设备上覆盖构建并且importance没有改变为 HIGH 。 I did not understand this until i came across this document .直到我遇到这个文件,我才明白这一点。

Importance levels have the following restrictions:重要性级别有以下限制:

The importance level you assign will be the channel's default.您分配的重要性级别将是频道的默认值。 Users can change a channel's importance level in Android Settings.用户可以在 Android 设置中更改频道的重要性级别。 Once you choose an importance level, you're limited in how you can change it: you may only lower the importance, and only if the user hasn't explicitly changed it.一旦你选择了一个重要性级别,你可以改变它的方式就会受到限制:你只能降低重要性,并且只有在用户没有明确更改它的情况下。

Conclusion :- Completely uninstall previous build or Clear Data(not sure about this) before upgrading the importance for notification channel.结论:- 在升级通知通道的重要性之前,完全卸载以前的构建或清除数据(不确定)。

You can do this way.. this will work on every android version你可以这样做..这适用于每个安卓版本

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);
}

You also need to set the App notification setting in "Settings" to Urgent.您还需要将“设置”中的应用通知设置设置为紧急。 A Heads up notification will not show up unless the User sets the App to urgent.除非用户将应用程序设置为紧急状态,否则不会显示抬头通知。

Heads up notifications works if you create Notification Channel and do something like this.如果您创建通知频道并执行类似操作,则提示通知有效。

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);

You are setting the wrong channel ID.您设置了错误的频道 ID。

setChannelId(String channelId) specifies the channel the notification should be delivered on. setChannelId(String channelId) 指定应该传递通知的通道。

And here in your example you are setting some id to your notification builder: setChannelId(id) and then creating a new secondary channel that you are not specifying: chan2在您的示例中,您正在为通知生成器设置一些idsetChannelId(id) ,然后创建一个您未指定的新辅助频道: chan2


Here is my working example:这是我的工作示例:

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());
    }

One more thing, make sure to REINSTALL your app after making these changes.还有一件事,请确保在进行这些更改后重新安装您的应用程序。

In some phones, there is a status bar and notification setting where if the top preview of notification is set to no preview , then it won't allow any app to show heads up, setting it to banner will solve your problem if you have already set up your channel and notification properly.在某些手机中,有一个状态栏和通知设置,如果通知的顶部预览设置为no preview ,那么它将不允许任何应用程序显示抬头,如果您已经将其设置为横幅将解决您的问题正确设置您的频道和通知。

I faced the same issue with the Android 9 OS, Vivo y1902 phone, which was resolved after making the above changes.我在 Android 9 操作系统 Vivo y1902 手机上遇到了同样的问题,在进行上述更改后已解决。

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

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