Включить прагматическое оповещение My Background Service - PullRequest
0 голосов
/ 19 ноября 2018

У меня есть приложение для Android, которое я должен запускать в фоновом режиме, используя службы для Android o и выше. Я знаю, что фоновые службы убиты системой, поэтому я использую startForground с соответствующими уведомлениями, но иногда эти уведомления не появляются, и это может быть из-за настроек мобильного телефона

так что если мы пойдем с

Настройки-> Приложение-> Имя моего приложения-> Уведомления-> Мои фоновые службы & Услуги

поэтому мой вопрос заключается в том, как мне начать или проверить эти «Мои справочные службы и сервисы» прагматично.

1 Ответ

0 голосов
/ 19 ноября 2018

С Android O,

Нам нужно установить идентификатор канала для каждого уведомления, поступившего в фоновом режиме.

Вы можете проверить последнюю реализацию Firebaseздесь.

В манифест необходимо добавить

<meta-data
    android:name="com.google.firebase.messaging.default_notification_channel_id"
    android:value="default_channel_id"/>

А в свой класс службы обмена сообщениями

 @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
     sendNotification(remoteMessage.getNotification().getBody());//Considering you have message in your body.
    }

И

private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        String channelId = getString(R.string.default_notification_channel_id);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.ic_stat_ic_notification)
                        .setContentTitle(getString(R.string.fcm_message))
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

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

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
...