首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重复的android提示通知

重复的android提示通知
EN

Stack Overflow用户
提问于 2018-03-14 19:22:03
回答 1查看 213关注 0票数 1

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

代码语言:javascript
复制
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版本中,它显示为正面通知,这是理所当然的,但只有第一个弹出,其余所有通知都在更新,但没有弹出。我想让用户看到的每一个通知都是平视的,完全可见。

EN

回答 1

Stack Overflow用户

发布于 2018-03-15 22:15:53

最后,我用startforeground调用完成了这个任务。它在5.1以上的版本中作为提醒通知工作,在以下版本中,它成功地显示了自动收报机通知。

代码语言:javascript
复制
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());

    }
}

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49276473

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档