我可以显示来自服务的提示通知。它会弹出第一个通知,并对用户可见。但此后,如果通知被更新,则它不会像第一个通知那样再次弹出,而是只发出通知声音并更新它,但不会像第一个通知那样再次弹出。
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版本中,它显示为正面通知,这是理所当然的,但只有第一个弹出,其余所有通知都在更新,但没有弹出。我想让用户看到的每一个通知都是平视的,完全可见。
发布于 2018-03-15 22:15:53
最后,我用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());
}
}
}https://stackoverflow.com/questions/49276473
复制相似问题