我能够显示该服务的抬头通知。它弹出第一个通知并且对用户可见。但此后如果通知被更新,那么它不再作为第一个弹出,而是只发出通知声音并更新它,但不会再次弹出。
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以后它显示为抬头通知,因为它应该是但只是首先弹出并且休息所有通知都会更新但不会弹出。我想让用户看到每个通知都是抬头并且完全可见。提前感谢
答案 0 :(得分:0)
最后我用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());
}
}
}