如何在安卓系统中显示悬浮通知

17
如何获取前置通知。使用以下代码,我只能在状态栏上看到三个点和一个通知栏中的通知。
Intent intent = new Intent(this, MainActivity.class);

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent,PendingIntent.FLAG_ONE_SHOT);
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.bip);
Uri defaultSoundUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.bip)
                .setContentTitle("Temp")
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0, notificationBuilder.build());

请查看他们的文档:http://developer.android.com/guide/topics/ui/notifiers/notifications.html - DNC
7
这是一个有些愚蠢的问题。需要进入设置,选择我的应用程序并更改应用程序通知设置。之后浮动通知即可显示。但是Facebook和其他应用程序如何在没有这些设置方面的问题的情况下运作呢? - user1837779
我猜测你正在使用一款中国制造的手机。网络上有迹象表明,这些手机在通知设置方面存在限制,而其他手机制造商则没有在其设备上设置这些限制。有些应用被列入白名单,可能是基于它们的受欢迎程度,很可能是基于手机制造商和应用程序提供商之间的付费协议。这对手机制造商来说是额外的收入来源,就像预装应用程序一样。 - Manuel
3个回答

17

这段代码对我来说可行:

NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(context)
                            .setSmallIcon(R.drawable.ic_media_play)
                            .setContentTitle("My notification")
                            .setContentText("Hello World!")
                            .setDefaults(Notification.DEFAULT_ALL)
                            .setPriority(Notification.PRIORITY_HIGH);

1
对于那些想知道为什么上面的代码起作用的人,请参见https://developer.android.com/guide/topics/ui/notifiers/notifications#Heads-up。 - Tura

16

我遇到了相同的问题,但我使用的是更新的NotificationCompat.Builder()调用,它需要来自NotificationChannel的通道ID。

只有当创建NotificationChannel的重要性值为NotificationManager.IMPORTANCE_HIGH时,通知才会以悬浮通知的形式出现:

NotificationChannel channel = new NotificationChannel("channel01", "name", 
     NotificationManager.IMPORTANCE_HIGH);   // for heads-up notifications
channel.setDescription("description");

// Register channel with system
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);

显示悬浮通知:

Notification notification = new NotificationCompat.Builder(this, "channel01")
        .setSmallIcon(android.R.drawable.ic_dialog_info)
        .setContentTitle("Test")
        .setContentText("You see me!")
        .setDefaults(Notification.DEFAULT_ALL)
        .setPriority(NotificationCompat.PRIORITY_HIGH)   // heads-up
        .build();

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(0, notification);

在安卓10上无法工作。 - Zeeshan Ali

0
这对我有用:https://dev59.com/QFwX5IYBdhLWcg3wrRI5#50296323 一个重要的事情是,一旦你像这样注册了通知系统:
// Register channel with system
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);

你无法再更改IMPORTANCE。根据我的经验,在更改通知的IMPORTANCE之后更改CHANNEL_ID可以使其显示为悬浮通知。


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