1

I would like to know if there is any way that helps to change application launcher icon based on the notification status it receives using xamarin.

Saamer
  • 4,687
  • 1
  • 13
  • 55
Green Computers
  • 723
  • 4
  • 14
  • 24

2 Answers2

2

You can't, at least not in iOS, the App Icon is defined in the Info.plist which is not writeable by the app, it's call bundled together by the developer - see https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html#//apple_ref/doc/uid/TP40009249-SW10

There's no official way of doing out in Android, and whilst there are a few hacks (not Xamarin specific), your mileage may vary per device - see How to change an application icon programmatically in Android?

Andy Flisher
  • 266
  • 1
  • 8
  • 4
    Not true, you can change iOS icon programatically now: https://stackoverflow.com/questions/43233675/ios-10-3-how-to-change-app-icon-programmatically – xleon Jun 17 '17 at 10:31
2

Yes you can change the "app icons" dynamically on both platforms now, but on iOS you can only change "app name" based on language preference!

iOS

Here's how to change app icon on iOS using Xamarin:

if (UIApplication.SharedApplication.SupportsAlternateIcons){
    await UIApplication.SharedApplication.SetAlternateIconNameAsync(iconName);
}

Android

Android app icon can be changed by adding an alias identical to your activity in the AndroidManifest and then just switching which one the app uses as shown here:

<activity android:name=".MainActivity">
    <intent-filter> 
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>  
</activity>
<activity-alias 
  android:name=".MainActivityAlias" 
  android:roundIcon="@drawable/ic_launcher_p_foreground"
  android:label="@string/app_name"
  android:icon="@mipmap/ic_launcher"
  android:enabled="false" 
  android:targetActivity=".MainActivity">  
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>  
</activity-alias>

and then run this code whenever you want to change the icon:

PackageManager.SetComponentEnabledSetting(
                new ComponentName(this, "{bundleId}.MainActivityAlias"),
                ComponentEnabledState.Enabled,
                ComponentEnableOption.DontKillApp);
PackageManager.SetComponentEnabledSetting(
                new ComponentName(this, "{bundleId}.MainActivity"),
                ComponentEnabledState.Disabled,
                ComponentEnableOption.DontKillApp);

You can use the same concept of Dependency Injection shown in the iOS article I shared above in order to change the android icon from your Xamarin forms code

Saamer
  • 4,687
  • 1
  • 13
  • 55