카테고리 보관물: Android

Android

Android 푸시 알림 : 알림에 아이콘이 표시되지 않고 흰색 사각형이 대신 표시됨 link, Bundle bundle) {

내 앱이 알림을 생성하지만 해당 알림에 설정 한 아이콘이 표시되지 않습니다. 대신 흰색 사각형이 나타납니다.

아이콘의 PNG 크기를 조정하려고했습니다 (치수 720×720, 66×66, 44×44, 22×22). 흥미롭게도 더 작은 치수를 사용할 때 흰색 사각형이 더 작습니다.

알림을 생성하는 올바른 방법뿐만 아니라이 문제를 봤으며 코드를 읽은 내용이 정확해야합니다. 안타깝게도 상황은 그렇지 않습니다.

내 휴대폰은 Android 5.1.1이 설치된 Nexus 5입니다. 에뮬레이터, Android 5.0.1의 Samsung Galaxy s4 및 Android 5.0.1의 Motorola Moto G에도 문제가 있습니다.

알림 코드와 두 개의 스크린 샷이 이어집니다. 자세한 정보가 필요하면 언제든지 문의하십시오.

다들 감사 해요.

@SuppressLint("NewApi") private void sendNotification(String msg, String title, String link, Bundle bundle) {
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
    resultIntent.putExtras(bundle);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            resultIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
    Notification notification;
    Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notificationsound);
    notification = new Notification.Builder(this)
                .setSmallIcon(R.drawable.lg_logo)
                .setContentTitle(title)
                .setStyle(new Notification.BigTextStyle().bigText(msg))
                .setAutoCancel(true)
                .setContentText(msg)
                .setContentIntent(contentIntent)
                .setSound(sound)
                .build();
    notificationManager.notify(0, notification);
}

알림을 열지 않고
알림이 열렸습니다



답변

원인 : 5.0 Lollipop의 경우 “알림 아이콘이 완전히 흰색이어야합니다”.

대상 SDK를 20으로 설정하여 흰색 아이콘 문제를 해결하면 앱에서 Android Lollipop을 타겟팅하지 않으므로 Lollipop 관련 기능을 사용할 수 없습니다.

대상 SDK 21 솔루션

Lollipop Material Icons를 지원하려면 Lollipop 이상 버전의 투명 아이콘을 만드십시오. https://design.google.com/icons/를 참조하십시오.

http://developer.android.com/design/style/iconography.html을 살펴보면 흰색 스타일이 Android Lollipop에 알림이 표시되는 방식임을 알 수 있습니다.

Lollipop에서 Google은 흰색 알림 아이콘 뒤에 표시 될 색상을 사용하도록 제안합니다. 링크 참조 : https://developer.android.com/about/versions/android-5.0-changes.html

색상을 추가하려는 모든 곳
https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setColor(int)

Lollipop OS 버전 이하의 Notification Builder 구현은 다음과 같습니다.

Notification notification = new NotificationCompat.Builder(this);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    notification.setSmallIcon(R.drawable.icon_transperent);
    notification.setColor(getResources().getColor(R.color.notification_color));
} else {
    notification.setSmallIcon(R.drawable.icon);
} 

참고 : setColor는 Lollipop에서만 사용할 수 있으며 아이콘의 배경에만 영향을줍니다.

그것은 당신의 문제를 완전히 해결할 것입니다!


답변

Google 클라우드 메시징을 사용하는 경우 단순히 아이콘을 변경 해도이 문제 가 해결되지 않습니다 . 예를 들어, 다음과 같이 작동하지 않습니다.

 Notification notification  = new Notification.Builder(this)
                .setContentTitle(title)
                .setContentText(text)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentIntent(pIntent)
                .setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS|Notification.DEFAULT_VIBRATE)
                .setAutoCancel(true)
                .build();

ic_notification이 투명하고 흰색 인 경우에도 마찬가지입니다. 다음과 같이 매니페스트 메타 데이터에도 정의해야합니다.

  <meta-data android:name="com.google.firebase.messaging.default_notification_icon"

            android:resource="@drawable/ic_notification" />

메타 데이터는 application태그 아래에 있습니다.


답변

Google의 디자인 가이드 라인을 따르는 것이 좋습니다 .

이는 말한다 “알림 아이콘이 완전히 흰색이어야합니다.”


답변

Android Manifest에서이 코드를 선언하십시오.

<meta-data android:name="com.google.firebase.messaging.default_notification_icon"

android:resource="@drawable/ic_stat_name" />

나는 이것이 당신에게 도움이되기를 바랍니다.


답변

(Android Studio 3.5) 최신 버전의 Android Studio 인 경우 알림 이미지를 생성 할 수 있습니다. res 폴더 > New> Image Asset을 마우스 오른쪽 버튼으로 클릭하십시오 . 그러면 아래 이미지와 같이 이미지 자산 구성이 표시됩니다. 아이콘 유형알림 아이콘으로 변경하십시오 . 이미지는 흰색이고 투명해야합니다. 이 이미지 자산 구성 은 해당 규칙을 시행합니다.
중요 사항 : 클라우드 / 푸시 알림에 아이콘을 사용하려면 새로 작성된 알림 아이콘을 사용하려면 애플리케이션 태그 아래에 메타 데이터를 추가해야합니다.이미지 자산 구성

  <application>
      ...
      <meta-data android:name="com.google.firebase.messaging.default_notification_icon"
          android:resource="@drawable/ic_notification" />


답변

우리는 아래와 같이 할 수 있습니다 :

setSmallIcon()아래 코드와 같이 알림 작성기 객체를 새로 작성하고 알림 작성기 객체를 사용하여 호출하십시오 .

앱을 설치할 OS 버전을 확인할 방법을 만듭니다. Lolipop, 즉 API 21 아래에 있으면 배경색이있는 일반 앱 아이콘이 사용되며 배경이없는 투명한 앱 아이콘이 사용됩니다. 따라서 os 버전> = 21을 사용하는 장치 setColor()는 알림 빌더 클래스의 메소드 를 사용하여 아이콘의 배경색을 설정합니다 .

샘플 코드 :

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);

notificationBuilder.setSmallIcon(getNotificationIcon(notificationBuilder));

private int getNotificationIcon(NotificationCompat.Builder notificationBuilder) {

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
             int color = 0x008000;
             notificationBuilder.setColor(color);
             return R.drawable.app_icon_lolipop_above;

    }
    return R.drawable.app_icon_lolipop_below;
}


답변

이 시도

나는 같은 문제에 직면했지만 많은 anwers를 시도했지만 해결책을 얻지 못했습니다. 마침내 문제를 해결할 방법을 찾았습니다.

-투명한 배경의 알림 아이콘 만들기. 앱의 너비와 높이는 아래 크기와 같아야하며 프로젝트-> 응용 프로그램-> src-> 메인-> 결과에 붙여 넣으십시오.

  • MDPI 24 * 24

  • HDPI 36 * 36

  • XHDPI 48 * 48

  • XXHDPI 72 * 72


위의 후에이 메시지를 onMessageReceived 메소드에 붙여 넣으십시오.


Intent intent = new Intent(this, News.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                    PendingIntent.FLAG_ONE_SHOT);
            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            {
                notificationBuilder.setSmallIcon(R.drawable.notify)
                                      //            .setContentTitle(title)
                            //                        .setContentText(message)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);
            } else
                {
                    notificationBuilder.setSmallIcon(R.drawable.notify)
                       //                                .setContentTitle(title)
                        //                        .setContentText(message)
                            .setAutoCancel(true)
                            .setSound(defaultSoundUri)
                            .setContentIntent(pendingIntent);
            }
            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0, notificationBuilder.build());

매니페스트 파일에이 코드를 추가하는 것을 잊지 마십시오

<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/app_icon" />