모바일 알림 통합

Android API 수준 26부터는 포그라운드 서비스 이 요구사항은 시스템 리소스에 과도한 부담을 줄 수 있는 서비스(예: 배터리 수명에 영향을 줍니다. 이러한 요구사항으로 인해 잠재적인 문제가 발생할 수 있습니다. 앱이 여러 개의 포그라운드 서비스가 있는 경우 알림을 주의 깊게 관리하지 않으므로 모든 서비스에서 공유된다는 사실을 기억하세요. 닫을 수 없는 알림으로, 활성 상태의 목록에 원치 않는 불필요한 알림을 있습니다.

이 문제는 Navigation과 같은 SDK를 사용할 때 더 어려워집니다. 포그라운드 서비스가 있는 앱과는 별개로 포그라운드 서비스를 실행하는 알림을 통합하기 어렵기 때문에 이를 통합하기가 어렵습니다. 이러한 문제를 해결하기 위해 Navigation SDK v1.11에는 SDK 내를 포함하여 앱 전체에서 지속적인 알림을 관리하는 데 도움이 됩니다.

지속적인 알림 통합

구성요소

포그라운드 서비스 관리자는 Android 포그라운드에 래퍼를 제공함 서비스 클래스와 영구 알림 클래스로 구성됩니다. 이 래퍼의 기본 함수는 알림 ID의 재사용을 시행하여 알림이 관리자를 사용하는 모든 포그라운드 서비스에서 공유됩니다.


Navigation SDK에는 ForegroundServiceManager 싱글톤 이 싱글톤은 초기화만 가능합니다. Navigation SDK 수명에 한 번 호출됩니다. 따라서 초기화 호출 (initForegroundServiceManagerMessageAndIntent() 또는 initForegroundServiceManagerProvider())이면 해당 경로가 다시 입력되는 경우 try-catch 블록으로 이 메서드를 호출합니다. Navigation SDK 두 번 이상 메서드를 호출하면 런타임 예외가 발생합니다. 먼저 ForegroundServiceManager에 대한 모든 참조를 지우고 다음을 호출합니다. 각 후속 호출 전에 clearForegroundServiceManager()입니다.

initForegroundServiceManagerMessageAndIntent()의 네 가지 매개변수는 다음과 같습니다. application, notificationId, defaultMessage, resumeIntent 만약 마지막 세 개의 매개변수가 null이면 알림은 표준 Navigation SDK 알림 다른 포그라운드를 숨기는 것도 가능합니다. 앱의 서비스와 관련된 정보를 표시합니다. notificationId 매개변수 알림에 사용되어야 하는 알림 ID를 지정합니다. 다음에 해당하는 경우 null이면 임의의 값이 사용됩니다. 문제를 해결하도록 명시적으로 설정할 수 있습니다. 다른 SDK의 알림 등과 같은 다른 알림과 충돌합니다. 이 defaultMessage는 시스템이 아니기 때문에 표시되는 문자열입니다. 탐색 중 resumeIntent는 알림이 표시될 때 실행되는 인텐트입니다. 알 수 있습니다. resumeIntent가 null이면 알림을 클릭합니다. 무시됩니다.

initForegroundServiceManagerProvider()의 세 가지 매개변수는 다음과 같습니다. application, notificationId, notificationProvider 만약 최종 두 매개변수가 null인 경우 알림은 표준 Navigation SDK입니다. 있습니다. notificationId 매개변수는 는 알림에 사용되어야 합니다. null인 경우 임의의 값은 있습니다. 다른 클라이언트와의 충돌을 해결하도록 명시적으로 설정할 수 있습니다. 알림을 받을 수 있습니다. notificationProvider가 다음과 같은 경우 가 설정되어 있는 경우 제공자는 항상 생성할 알림을 생성합니다.

Navigation SDK getForegroundServiceManager() 메서드는 포그라운드 서비스 관리자 싱글톤 아직 보고서를 생성하지 않았다면 initForegroundServiceManagerMessageAndIntent()를 호출하는 것과 같습니다. notificationId, defaultMessage, resumeIntent입니다.

ForegroundServiceManager에는 간단한 세 가지 메서드가 있습니다. 처음 두 가지는 서비스를 포그라운드 안팎으로 이동하는 작업을 의미하며, 일반적으로 생성된 서비스 내에서만 사용할 수 있습니다 이러한 메서드를 사용하면 서비스는 공유된 영구 알림과 연결됩니다. 마지막 updateNotification() 메서드는 알림이 있는 관리자를 플래그 지정합니다. 다시 렌더링되어야 합니다.

공유된 지속적 알림을 완전히 제어해야 한다면 API는 NotificationContentProvider 인터페이스를 제공하여 알림을 받는 단일 메서드가 포함된 알림 제공자 합쳐야 합니다. 또한 기본 클래스를 제공하여 선택적으로 제공자를 정의하는 데 사용됩니다. 기본 클래스의 기본 클래스 중 하나 목적은 호출하지 않고 updateNotification()를 호출하는 방법을 제공하는 것입니다. ForegroundServiceManager에 액세스해야 합니다. 포드의 인스턴스를 새 알림 메시지를 수신하려면 이 메서드를 내부 메서드를 직접 호출하여 알림에서 메시지를 렌더링할 수 있습니다.

사용 시나리오

이 섹션에서는 공유 영구 항목 사용을 위한 사용 시나리오를 자세히 설명합니다. 있습니다.

다른 앱 포그라운드 서비스의 지속적인 알림 숨기기
가장 쉬운 시나리오는 현재 동작을 유지하고 Navigation SDK 정보 렌더링을 위한 영구 알림 기타 서비스 포그라운드 서비스 관리자를 사용하여 이 알림 뒤에 숨길 수 있습니다. startForeground()stopForeground() 메서드
다른 앱 포그라운드 서비스의 지속적인 알림을 숨기지만 설정됨 내비게이션을 사용하지 않을 때 표시되는 기본 텍스트
두 번째로 쉬운 시나리오는 현재 동작을 유지하고 Navigation SDK 정보 렌더링을 위한 영구 알림(단, 확인할 수 있습니다. 시스템이 탐색 중이 아니면 initForegroundServiceManagerMessageAndIntent()에 제공된 문자열 가 'Google 지도' 또한 이 호출을 사용하여 알림이 클릭되면 실행됩니다.
지속적인 알림의 렌더링을 완전히 제어
마지막 시나리오에서는 알림 제공자를 정의하고 만들어야 합니다. 를 사용하여 ForegroundServiceManager initForegroundServiceManagerProvider()입니다. 이 옵션을 사용하면 알림에서 렌더링되는 내용을 완전히 제어할 수 있을 뿐만 아니라 Navigation SDK 알림 정보를 이에 따라 지도에 표시되는 유용한 세부 경로 안내 메시지를 있습니다. Google에서는 이 정보를 검색할 수 있는 간단한 방법을 제공하지 않습니다. 알림에 삽입할 수 있습니다.

알림 제공자 예시

다음 코드 예에서는 알림을 만들고 반환하는 방법을 보여줍니다. 간단한 알림 콘텐츠 제공자를 사용합니다.

public class NotificationContentProviderImpl
   extends NotificationContentProviderBase
   implements NotificationContentProvider {
 private String channelId;
 private Context context;
 private String message;

 /** Constructor */
 public NotificationContentProviderImpl(Application application) {
   super(application);
   message = "-- uninitialized --";
   channelId = null;
   this.context = application;
 }

 /**
  * Sets message to display in the notification. Calls updateNotification
  * to display the message immediately.
  *
  * @param msg The message to display in the notification.
  */
 public void setMessage(String msg) {
   message = msg;
   updateNotification();
 }

 /**
  * Returns the notification as it should be rendered.
  */
 @Override
 public Notification getNotification() {
   Notification notification;

   if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
     Spanned styledText = Html.fromHtml(message, FROM_HTML_MODE_LEGACY);
     String channelId = getChannelId(context);
     notification =
         new Notification.Builder(context, channelId)
             .setContentTitle("Notifications Demo")
             .setStyle(new Notification.BigTextStyle()
                 .bigText(styledText))
             .setSmallIcon(R.drawable.ic_navigation_white_24dp)
             .setTicker("ticker text")
             .build();
   } else {
     notification = new Notification.Builder(context)
         .setContentTitle("Notification Demo")
         .setContentText("testing non-O text")
         .build();
   }

   return notification;
 }

 // Helper to set up a channel ID.
 private String getChannelId(Context context) {
   if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
     if (channelId == null) {
       NotificationManager notificationManager =
           (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
       NotificationChannel channel = new NotificationChannel(
           "default", "navigation", NotificationManager.IMPORTANCE_DEFAULT);
       channel.setDescription("For navigation persistent notification.");
       notificationManager.createNotificationChannel(channel);
       channelId = channel.getId();
     }
     return channelId;
   } else {
     return "";
   }
 }
}

NotificationContentProviderImpl를 만든 후 Navigation SDK를 추가합니다.

ForegroundServiceManager f = NavigationApi.getForegroundServiceManager(getApplication());
mNotification = new NotificationContentProviderImpl(getApplication());
NavigationApi.clearForegroundServiceManager();
NavigationApi.initForegroundServiceManagerProvider(getApplication(), null, mNotification);

주의사항 및 향후 계획

  • initForegroundServiceManagerMessageAndIntent() 또는 initForegroundServiceManagerProvider()를 일찍 호출하여 잘 정의된 것입니다 이 메서드를 호출해야 합니다. 새 탐색기를 만들기 전에 확인해야 합니다
  • 호출에서 예외를 포착해야 합니다. 코드 경로가 다음과 같은 경우 initForegroundServiceManagerMessageAndIntent() 또는 initForegroundServiceManagerProvider() 두 번 이상 입력했습니다. Navigation SDK v2.0에서 이 메서드 호출 런타임 예외가 아닌 확인된 예외가 발생합니다.
  • Google에서 헤더 스타일과 일치하는 알림의 전체 기간입니다.
  • 알림 제공자를 정의할 때 헤드업 동작을 제어할 수 있습니다. 우선순위를 갖습니다
  • Google은 세부 경로 안내를 검색하는 간단한 수단을 제공하지 않습니다. 알림 제공자가 알림에 삽입할 수 있는 정보입니다.