繁体   English   中英

重复 android 抬头通知

[英]Repeating android heads up notifications

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


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());

    }
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM