تغيير أيقونة تطبيق أندرويد
لتغيير أيقونة التطبيق بناءً على عدد الإشعارات في مركز الإشعارات، اتبع الخطوات أدناه.
- أضف أيقونة جديدة إلى مشروعك في مجلد ‘mipmap’.
- أنشئ فئة Activity جديدة.
import android.os.Bundle;import androidx.appcompat.app.AppCompatActivity;
public class MainActivityAlias extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
}}- أنشئ فئة تحتوي على منطق تغيير الأيقونة.
import android.content.ComponentName;import android.content.Context;import android.content.pm.PackageManager;
public class MyPushReceiver {
public static void changeAppIcon(Context context, int count) { PackageManager pm = context.getPackageManager(); if (count > 0) { pm.setComponentEnabledSetting( new ComponentName(context, "com.example.app.MainActivityAlias"), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP ); pm.setComponentEnabledSetting( new ComponentName(context, "com.example.app.MainActivity"), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP ); } else { pm.setComponentEnabledSetting( new ComponentName(context, "com.example.app.MainActivity"), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP ); pm.setComponentEnabledSetting( new ComponentName(context, "com.example.app.MainActivityAlias"), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP ); } }}- في فئة
NotificationServiceExtensionالخاصة بك، قم بتنفيذ تتبع استلام/حذف إشعارات الدفع.
@Overridepublic boolean onMessageReceived(final PushMessage message) { super.onMessageReceived(message);
if (!message.isSilent()) { /** * في هذا المثال، يتم الإشارة ببساطة إلى عدد عشوائي من الإشعارات. * تحتاج إلى تنفيذ منطق حساب الإشعارات بنفسك. */ int notificationCount = 1;
if (getApplicationContext() != null) { MyPushReceiver.changeAppIcon(getApplicationContext(), notificationCount); } }}
/*** رد نداء Pushwoosh لتتبع إزالة إشعارات الدفع من مركز الإشعارات.*/@Overrideprotected void onMessageCanceled(PushMessage message) { super.onMessageCanceled(message);
if (!message.isSilent()) { /** * في هذا المثال، يتم الإشارة ببساطة إلى عدد عشوائي من الإشعارات. * تحتاج إلى تنفيذ منطق حساب الإشعارات بنفسك. */ int notificationCount = 0;
if (getApplicationContext() != null) { MyPushReceiver.changeAppIcon(getApplicationContext(), notificationCount); } }}- تم!