gpt4 book ai didi

android 在任何应用程序顶部显示带有弹出窗口的通知

转载 作者:IT王子 更新时间:2023-10-29 00:00:47 25 4
gpt4 key购买 nike

使用下面的代码,我的通知只会添加到通知栏,不会显示弹出式消息,就像您在另一个应用程序中时会收到 whatsapp 消息一样。是什么导致通知发生这种情况?

private void sendNotification(int distance, ViewObject viewObject) {
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.putExtra("path", viewObject.getPath());
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent notificationPendingIntent = stackBuilder.getPendingIntent(Integer.parseInt(viewObject.getRefId()), PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
bigText.bigText(String.format(getString(R.string.notification), viewObject.getTitle()));
bigText.setBigContentTitle(getString(R.string.hello));

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_wald_poi)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_poi))
.setColor(getResources().getColor(R.color.primary))
.setContentTitle(getString(R.string.hello))
.setContentIntent(notificationPendingIntent)
.setContentText(String.format(getString(R.string.notification), viewObject.getTitle()))
.setDefaults(Notification.DEFAULT_ALL)
.setStyle(bigText);

builder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, builder.build());
}

最佳答案

如果你想像这样使用 Heads-up Notifications:

enter image description here

您必须更改 Notification 优先级或 NotificationChannel 重要性。

通知优先级,由 setPriority() 设置。优先级决定了通知在 Android 7.1 及更低版本上的干扰程度。 (对于 Android 8.0 及更高版本,您必须改为设置 channel 重要性)

在 Android 7.1(API 级别 25)及更低版本上:

  • 将通知优先级设置为 NotificationCompat.PRIORITY_HIGHNotificationCompat.PRIORITY_MAX
  • 设置铃声和振动 - 你可以使用 setDefaults(Notification.DEFAULT_ALL)

Android 8.0(API 级别 26)及更高版本:

  • 将通知 channel 优先级设置为 NotificationManager.IMPORTANCE_HIGH

通知:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_wald_poi)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_poi))
.setColor(getResources().getColor(R.color.primary))
.setContentTitle(getString(R.string.hello))
.setContentIntent(notificationPendingIntent)
.setContentText(String.format(getString(R.string.notification), viewObject.getTitle()))
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setStyle(bigText)
.setPriority(NotificationCompat.PRIORITY_HIGH) // or NotificationCompat.PRIORITY_MAX

通知 channel :

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create the NotificationChannel
val name = getString(R.string.notification_channel_name)
val descriptionText = getString(R.string.notification_channel_description)
val importance = NotificationManager.IMPORTANCE_HIGH
val mChannel = NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance)
mChannel.description = descriptionText
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(mChannel)
}

重要

If you'd like to further customize your channel's default notification behaviors, you can call methods such as enableLights(), setLightColor(), and setVibrationPattern() on the NotificationChannel. But remember that once you create the channel, you cannot change these settings and the user has final control of whether these behaviors are active. Other option is to uninstall and install application again. Read more


Examples of conditions that may trigger heads-up notifications include:

  • The user's activity is in fullscreen mode (the app uses fullScreenIntent).
  • The notification has high priority and uses ringtones or vibrations on devices running Android 7.1 (API level 25) and lower.
  • The notification channel has high importance on devices running Android 8.0 (API level 26) and higher.

优先级:

Notification.PRIORITY_HIGH and Notification.PRIORITY_MAX was deprecated in API level 26. use NotificationCompat instead.

Here更多信息:-)

关于android 在任何应用程序顶部显示带有弹出窗口的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29949501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com