Как показать Alert как истинного абонента, даже если приложение близко? - PullRequest
1 голос
/ 18 января 2020

Я разрабатываю приложение SOS.

При получении уведомления в приложении Android я хочу открыть всплывающее окно с предупреждением, в котором будут отображаться некоторые детали значков отправителя и SOS, а также прозвучит непрерывный звуковой сигнал.

Я делал это с помощью приведенного ниже кода, но на android 9 Деятельность не начинается. И этот код не запускает действие, когда приложение находится в состоянии «завершено» и не работает в фоновом режиме.

private void sendNotification(String messageBody, String notificationType, String notificationId) {       

    try {
        Intent intent = new Intent(this, SosPopUpActivity.class);

        intent.putExtra("message", messageBody);
        intent.putExtra("notification_type", notificationType);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

        NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel mChannel = new NotificationChannel(
                    channelId, channelName, importance);
            notificationManager.createNotificationChannel(mChannel);
        }


        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        //  NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.ic_notification)
                .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher))
                .setContentTitle(getString(R.string.app_name))
                .setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
                .setContentText(messageBody)
                .setAutoCancel(false)
                .setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getApplicationContext().getPackageName() + "/" + R.raw.sos))

                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);


        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            notificationBuilder.setSmallIcon(R.drawable.ic_notification);
            notificationBuilder.setColor(getResources().getColor(R.color.colorPrimary));
            //notificationBuilder.setColor(getResources().getColor(R.color.notification_color));
        } else {
            notificationBuilder.setSmallIcon(R.drawable.ic_notification);
        }

        notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);

        //  NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // notificationManager.notify(1234, notificationBuilder.build());

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addNextIntent(intent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
        );
        notificationBuilder.setContentIntent(resultPendingIntent);

        notificationManager.notify(1234, notificationBuilder.build());

        startActivity(intent);
    }catch (Exception e)
    {
        e.printStackTrace();
        AlertDialogHelper.showAlertDialog(this,e.getLocalizedMessage());
    }

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...