Programing

앱 런처 아이콘에 알림 수를 표시하는 방법

lottogame 2020. 5. 18. 08:01
반응형

앱 런처 아이콘에 알림 수를 표시하는 방법


이 질문에는 이미 답변이 있습니다.

삼성 갤럭시 노트 2 안드로이드 버전 4.1.2

이 질문은 이전에 요청되었으며 답변이 불가능하다는 것을 알고 있습니다.

안드로이드에서 응용 프로그램 실행기 아이콘 위에 풍선 카운터를 표시하는 방법

그럼에도 불구하고 어제 페이스 북 앱을 업데이트했으며 읽지 않은 메시지 개인 메시지 카운터를 표시하기 시작했습니다. 페이스 북 앱을 어떻게 사용할 수 있고 내 앱에서 그렇게 할 수 없습니까?

페이스 북 아이콘

여기에 이미지 설명을 입력하십시오

삼성 갤럭시 노트 2 안드로이드 버전 4.1.2


Android (맞춤형 실행기 및 터치 인터페이스 가없는 "vanilla"android) .apk 프로그램을 컴파일 한 후에는 봉인되어 있기 때문에 애플리케이션 아이콘을 변경할 수 없습니다 . 표준 API를 사용하여 프로그래밍 방식으로 '드로어 블'로 변경할 수있는 방법이 없습니다. 아이콘 대신 위젯을 사용하여 목표를 달성 할 수 있습니다. 위젯은 사용자 정의 할 수 있습니다. http://www.cnet.com/8301-19736_1-10278814-251.html 및이 http://developer.android.com/guide/topics/appwidgets/index.html을 읽어보십시오 . 또한 이쪽을 봐 : https://github.com/jgilfelt/android-viewbadger . 도움이 될 수 있습니다.

배지 번호는. 내가 전에 말했듯이-이것을하는 표준 방법은 없습니다. 그러나 우리는 모두 Android가 개방형 운영 체제라는 것을 알고 있으며 원하는 모든 작업을 수행 할 수 있으므로 배지 번호를 추가하는 유일한 방법은 타사 앱 또는 맞춤형 런처를 사용하거나 프런트 엔드 터치를 사용하는 것입니다 인터페이스 : Samsung TouchWiz 또는 Sony Xperia의 인터페이스. 다른 답변은이 기능을 사용하며 스택 오버 플로우에서이를 검색 할 수 있습니다 (예 : here) . 그러나 나는 한 번 더 반복 할 것입니다 : 이것에 대한 표준 API 없으며 그것이 나쁜 습관 이라고 말하고 싶습니다 . 앱의 아이콘 알림 배지는 iOS 패턴이므로 Android 앱에서는 사용해서는 안됩니다. Andrioid에는 다음과 같은 상태 표시 줄 알림이 있습니다.http://developer.android.com/guide/topics/ui/notifiers/notifications.html 따라서 Facebook 또는 다른 사람이 이것을 사용하는 경우 일반적인 패턴이나 추세가 아닙니다. 그러나 어쨌든 주장하고 홈 화면 위젯을 사용하지 않으려면 여기를보십시오.

Facebook은 Android의 앱 아이콘에 배지 번호를 어떻게 추가합니까?

보시다시피 이것은 실제 Facebook 앱이 아닙니다. TouchWiz입니다. 바닐라 안드로이드에서는 Nova Launcher를 사용 하여이 작업을 수행 할 수 있습니다 http://forums.androidcentral.com/android-applications/199709-how-guide-global-badge-notifications.html 따라서 어딘가에 아이콘 배지가 표시되는지 확인하십시오. 타사 실행기 또는 터치 인터페이스 (프런트 엔드 랩퍼) 언젠가 Google이이 기능을 표준 Android API에 추가 할 수 있습니다.


그것은 삼성 touchwiz 실행기에서 작동

public static void setBadge(Context context, int count) {
    String launcherClassName = getLauncherClassName(context);
    if (launcherClassName == null) {
        return;
    }
    Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
    intent.putExtra("badge_count", count);
    intent.putExtra("badge_count_package_name", context.getPackageName());
    intent.putExtra("badge_count_class_name", launcherClassName);
    context.sendBroadcast(intent);
}

public static String getLauncherClassName(Context context) {

    PackageManager pm = context.getPackageManager();

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);

    List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
    for (ResolveInfo resolveInfo : resolveInfos) {
        String pkgName = resolveInfo.activityInfo.applicationInfo.packageName;
        if (pkgName.equalsIgnoreCase(context.getPackageName())) {
            String className = resolveInfo.activityInfo.name;
            return className;
        }
    }
    return null;
}

ShortcutBadger 는 장치 브랜드 및 현재 실행기에 추상화 계층을 추가하고 훌륭한 결과를 제공하는 라이브러리입니다. LG, Sony, Samsung, HTC 및 기타 맞춤형 런처와 함께 작동합니다.

Pure Android 장치 데스크탑에 배지 수를 표시하는 방법도 있습니다.

응용 프로그램 아이콘에서 배지 수를 업데이트하는 것은 다음과 같이 쉽습니다.

int badgeCount = 1;
ShortcutBadger.applyCount(context, badgeCount);

여기에는 동작을 테스트 할 수있는 데모 응용 프로그램이 포함되어 있습니다.


나는 이것이 Sony 장치에서 어떻게 수행되는지 알아 냈습니다.

나는 그것에 대해 블로그에 올렸 습니다 . 나는 또한 이것에 대해 별도의 SO 질문을 게시 한 여기 .


Sony 장치는라는 클래스를 사용합니다 BadgeReciever.

  1. com.sonyericsson.home.permission.BROADCAST_BADGE매니페스트 파일에서 권한을 선언 하십시오.

  2. 방송 Intent받는 사람을 BadgeReceiver:

    Intent intent = new Intent();
    
    intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
    intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", "com.yourdomain.yourapp.MainActivity");
    intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", true);
    intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", "99");
    intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", "com.yourdomain.yourapp");
    
    sendBroadcast(intent);
    
  3. 끝난. Intent방송이 시작 되면 실행기는 응용 프로그램 아이콘에 배지를 표시해야합니다.

  4. 배지를 다시 제거하려면 이번에는 SHOW_MESSAGEfalse 설정하여 새 브로드 캐스트를 보내기 만하면 됩니다.

    intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", false);
    

답을 짧게 유지하기 위해 이것을 찾은 방법에 대한 세부 사항을 제외했지만 블로그에서 모두 사용할 수 있습니다. 누군가에게 흥미로운 읽을 거리가 될 수 있습니다.


알림 실행기 아이콘에 배지를 표시하는 가장 좋은 방법입니다.

애플리케이션에이 클래스 추가

public class BadgeUtils {

    public static void setBadge(Context context, int count) {
        setBadgeSamsung(context, count);
        setBadgeSony(context, count);
    }

    public static void clearBadge(Context context) {
        setBadgeSamsung(context, 0);
        clearBadgeSony(context);
    }


    private static void setBadgeSamsung(Context context, int count) {
        String launcherClassName = getLauncherClassName(context);
        if (launcherClassName == null) {
            return;
        }
        Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
        intent.putExtra("badge_count", count);
        intent.putExtra("badge_count_package_name", context.getPackageName());
        intent.putExtra("badge_count_class_name", launcherClassName);
        context.sendBroadcast(intent);
    }

    private static void setBadgeSony(Context context, int count) {
        String launcherClassName = getLauncherClassName(context);
        if (launcherClassName == null) {
            return;
        }

        Intent intent = new Intent();
        intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
        intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", launcherClassName);
        intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", true);
        intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", String.valueOf(count));
        intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());

        context.sendBroadcast(intent);
    }


    private static void clearBadgeSony(Context context) {
        String launcherClassName = getLauncherClassName(context);
        if (launcherClassName == null) {
            return;
        }

        Intent intent = new Intent();
        intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
        intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", launcherClassName);
        intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", false);
        intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", String.valueOf(0));
        intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());

        context.sendBroadcast(intent);
    }

    private static String getLauncherClassName(Context context) {

        PackageManager pm = context.getPackageManager();

        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
        for (ResolveInfo resolveInfo : resolveInfos) {
            String pkgName = resolveInfo.activityInfo.applicationInfo.packageName;
            if (pkgName.equalsIgnoreCase(context.getPackageName())) {
                String className = resolveInfo.activityInfo.name;
                return className;
            }
        }
        return null;
    }


}

==> MyGcmListenerService.java 알림이 오면 BadgeUtils 클래스를 사용하십시오.

public class MyGcmListenerService extends GcmListenerService { 

    private static final String TAG = "MyGcmListenerService"; 
    @Override
    public void onMessageReceived(String from, Bundle data) {

            String message = data.getString("Msg");
            String Type = data.getString("Type"); 
            Intent intent = new Intent(this, SplashActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                    PendingIntent.FLAG_ONE_SHOT);

            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

            NotificationCompat.BigTextStyle bigTextStyle= new NotificationCompat.BigTextStyle();

            bigTextStyle .setBigContentTitle(getString(R.string.app_name))
                    .bigText(message);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(getNotificationIcon())
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText(message)
                    .setStyle(bigTextStyle) 
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);

            int color = getResources().getColor(R.color.appColor);
            notificationBuilder.setColor(color);
            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


            int unOpenCount=AppUtill.getPreferenceInt("NOTICOUNT",this);
            unOpenCount=unOpenCount+1;

            AppUtill.savePreferenceLong("NOTICOUNT",unOpenCount,this);  
            notificationManager.notify(unOpenCount /* ID of notification */, notificationBuilder.build()); 

// This is for bladge on home icon          
        BadgeUtils.setBadge(MyGcmListenerService.this,(int)unOpenCount);

    }


    private int getNotificationIcon() {
        boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
        return useWhiteIcon ? R.drawable.notification_small_icon : R.drawable.icon_launcher;
    }
}

선호도 및 배지 수와 함께 명확한 알림

 public class SplashActivity extends AppCompatActivity { 
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_splash);

                    AppUtill.savePreferenceLong("NOTICOUNT",0,this);
                    BadgeUtils.clearBadge(this);
            }
    }
<uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE" />

참고 URL : https://stackoverflow.com/questions/17565307/how-to-display-count-of-notifications-in-app-launcher-icon

반응형