借助前台服务,抬头通知将停留在屏幕上

时间:2019-07-05 21:16:06

标签: java android notifications foreground-service heads-up-notifications

我的平视通知是从前台服务发布的。它们可以在多个旧版本的Android(4.4、7.0、9.0)上正常运行,但在Android 5上除外,在Android 5上会弹出抬头通知,然后在屏幕上“卡住”。

我的项目针对Android版本27进行编译,而minSdkVersion设置为18。

我已经使用IMPORTANCE_HIGH创建了通知渠道。我使用PRIORITY_HIGH构建通知,并成功生成带有声音和振动的抬头通知。

直到现在,我一直在使用单个通知ID,以启动前台服务通知,并使用新数据更新同一通知。

在我的Android 5平台上,显示抬头通知,然后停留在屏幕上。我可以向侧面或上方滑动,以将通知移至弹出式抽屉。如果我点击“卡住”通知,则会触发该通知的 ContentIntent ,并显示我的活动,但现在前台服务的正在进行中通知已消失(仍在运行)。

到目前为止,对于Android 5平台,显示正确的抬头通知的唯一方法是使用2个不同的通知ID,一个ID启动前台服务,第二个Notification ID更新相同的通知,包含新数据。 (但这会创建2条通知

如果我根本不在前台启动该服务,则使用单个通知ID,显示单个抬头通知,然后在5秒钟后消失(<应该> )。 / p>

    @Override
    public void onCreate() {
        super.onCreate();
        startForeground(NOTIFY_ID_SERVICE, createNotification(getString(R.string.CHANNEL_ID_FOUND),
                                                getString(R.string.notification_title_default),
                                                getString(R.string.msg_Service_is_enabled),
                                                NotificationCompat.CATEGORY_SERVICE,
                                                NotificationCompat.PRIORITY_DEFAULT,
                                                false).build());
    }

    private NotificationCompat.Builder createNotification(String channelId, String title, String text, String category, int priority, boolean isOngoing) {

        Intent resultIntent = new Intent(this, MainActivity.class);
        resultIntent.setAction(Intent.ACTION_MAIN);
        resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);


        NotificationCompat.Builder builder = null;
        try {
            builder = new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(GlobalApp.getAppContext().getApplicationInfo().icon)
                    .setContentTitle(title)
                    .setCategory(category)
                    .setContentText(text)
                    .setOngoing(isOngoing)
                    .setContentIntent(pendingIntent)
                    .setPriority(priority);
        }
        catch(Exception e)
        {
            Log.e(GlobalApp.getLogTag(), "createNotification error: " + e.getMessage());
        }

        return builder;
    }

    private void postNotification(boolean isFound, String elapsedTime) {

        NotificationCompat.Builder builder;

        try {

            builder = createNotification(getString(R.string.CHANNEL_ID_FOUND),
                                getString(R.string.notification_title_default),
                                getString(R.string.msg_internet_found) + "   Elapsed: " + elapsedTime,
                                NotificationCompat.CATEGORY_STATUS,
                                NotificationCompat.PRIORITY_HIGH,
                                false);


            if (isFound) {

                if(GlobalApp.isVibrateEnabled()) {
                    builder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
                }

                if(GlobalApp.getRingtoneFound() != null) {
                    builder.setSound(Uri.parse(GlobalApp.getRingtoneFound()));
                } else {
                    builder.setDefaults(NotificationCompat.DEFAULT_SOUND);
                }

            } else {
                builder = createNotification(getString(R.string.CHANNEL_ID_LOST),
                                getString(R.string.notification_title_default),
                                getString(R.string.msg_internet_lost) + "   Elapsed: " + elapsedTime,
                                NotificationCompat.CATEGORY_STATUS,
                                NotificationCompat.PRIORITY_HIGH,
                                false);


                if(GlobalApp.getConnectionStatus() == GlobalApp.CONNECTION_STATUS_WIFI_LOST) {
                    builder.setContentText(getString(R.string.msg_wifi_lost) + "   Elapsed: " + elapsedTime);
                } else {
                    builder.setContentText(getString(R.string.msg_internet_lost) + "   Elapsed: " + elapsedTime);
                }

                if(GlobalApp.getRingtoneLost() != null) {
                    builder.setSound(Uri.parse(GlobalApp.getRingtoneLost()));
                } else {
                    builder.setDefaults(NotificationCompat.DEFAULT_SOUND);
                }
            }

            if(GlobalApp.isVibrateEnabled()) {
                builder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
            }

            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(GlobalApp.getAppContext());

            notificationManager.notify(NOTIFY_ID_SERVICE, builder.build());
        }
        catch(Exception e)
        {
            Log.e(GlobalApp.getLogTag(), "Notification error: " + e.getMessage());
        }
    }

0 个答案:

没有答案