gpt4 book ai didi

java - 重复 Android 抬头通知

转载 作者:太空宇宙 更新时间:2023-11-04 10:38:15 26 4
gpt4 key购买 nike

我能够显示来自该服务的提醒通知。它会弹出第一个通知并对用户可见。但此后,如果通知更新,则它不会再次像第一个通知那样弹出,而只会发出通知声音并更新它,但不会像第一次一样再次弹出。

<小时/>
Showing very first notification from service as below : 

public class WatchMan extends Service
{
NotificationManager mNotifyManager;
NotificationCompat.Builder mBuilder;
NotificationChannel notificationChannel;
String NOTIFICATION_CHANNEL_ID = "1";

public boolean Notif_Seven = false;
public boolean Notif_Eight = false;

public WatchMan() { }

@Override
public void onCreate()
{
try
{
mNotifyManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(this, null);
mBuilder.setContentTitle("App Title")
.setContentText("Up and Monitoring..")
.setTicker("Up and Monitoring..")
.setSmallIcon(R.drawable.ic_service_success)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_HIGH)
.setOnlyAlertOnce(false)
.setAutoCancel(true);

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

// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
mNotifyManager.createNotificationChannel(notificationChannel);
}

mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotifyManager.notify(1, mBuilder.build());
startForeground(1, mBuilder.build());


}
catch(Exception e)
{
Log.d(TAG, "EXCEPTION IN SHOWING NOTIFICATION...\n");
Log.e(TAG, "Exception is : ", e);
}
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{

// STICKY Runnable thread WHILE ( TRUE )
// my code goes here with multiple conditions checking
// Say condition 7 is false and want to notify user again.

if (!Notif_Seven)
{
Notif_Seven = true;
mBuilder.setContentText("SET DEFAULT TYPE IN SETTINGS..");
mBuilder.setTicker("SET DEFAULT TYPE IN SETTINGS..");
mNotifyManager.notify(1, mBuilder.build());

}

Thread.sleep(10000);
continue;

// Say condition 8 is false and want to notify user again.

if (!Notif_Eight)
{
Notif_Eight = true;
mBuilder.setContentText("SET PERCENTAGE SETTINGS..");
mBuilder.setTicker("SET PERCENTAGE SETTINGS..");
mNotifyManager.notify(1, mBuilder.build());

}
}

}
<小时/>

它在 4.1 中一一显示多个通知滚动条,但在 5.1 中,它显示为平视通知,因为它应该是这样,但只有第一个弹出,其余所有通知都会更新但不会弹出。我想让用户看到每个通知都是抬头且完全可见的。

最佳答案

最后我通过 startforeground 调用完成了它。它在 5.1 以上版本中作为抬头通知工作,在以下版本中它可以成功显示股票通知。

<小时/>
public class WatchMan extends Service
{
NotificationManager mNotifyManager;
NotificationCompat.Builder mBuilder;
NotificationChannel notificationChannel;
String NOTIFICATION_CHANNEL_ID = "1";

public boolean Notif_Seven = false;
public boolean Notif_Eight = false;

public WatchMan() { }

@Override
public void onCreate()
{
try
{
mNotifyManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(this, null);
mBuilder.setContentTitle("App Title")
.setContentText("Up and Monitoring..")
.setTicker("Up and Monitoring..")
.setSmallIcon(R.drawable.ic_service_success)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_HIGH)
.setOngoing(true);

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

// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
mNotifyManager.createNotificationChannel(notificationChannel);
}

mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
//mNotifyManager.notify(1, mBuilder.build());
startForeground(1, mBuilder.build());


}
catch(Exception e)
{
Log.d(TAG, "EXCEPTION IN SHOWING NOTIFICATION...\n");
Log.e(TAG, "Exception is : ", e);
}
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{

// STICKY Runnable thread WHILE ( TRUE )
// my code goes here with multiple conditions checking
// Say condition 7 is false and want to notify user again.

if (!Notif_Seven)
{
Notif_Seven = true;
mBuilder.setContentText("SET DEFAULT TYPE IN SETTINGS..");
mBuilder.setTicker("SET DEFAULT TYPE IN SETTINGS..");
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
startForeground(1, mBuilder.build());

}

Thread.sleep(10000);
continue;

// Say condition 8 is false and want to notify user again.

if (!Notif_Eight)
{
Notif_Eight = true;
mBuilder.setContentText("SET PERCENTAGE SETTINGS..");
mBuilder.setTicker("SET PERCENTAGE SETTINGS..");
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
startForeground(1, mBuilder.build());

}
}

}

关于java - 重复 Android 抬头通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49276473/

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