Question

I'm currently developing a native app for both Android and iOS. One of the requirements is that the app icon has a red badge with number that is common on iOS devices. On iOS I will update the app badge via a push notification. For Android I plan to create a widget to accomplish this. I'm thinking the widget will be similar to facebook's.

My question is...Can an Android widget be updated with a GCM notification? If not, is there an alternative solution to my problem? I know that Samsung devices can have badges by using BadgeProvider, but I need this to work on all Android devices.

enter image description here

Was it helpful?

Solution

Yes, you can update your Widgets in reaction to a push notification.

Assuming that your push notification kicks off a service or something, you might have a method like so:

private void updateWidgets() {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);

    // Get a list of the widgets that need updating
    int[] widgetIds = appWidgetManager.getAppWidgetIds(
        new ComponentName(this, MyWidgetProvider.class)
    );

    RemoteViews updatedViews = new RemoteViews(); // Actually get the updated views
    appWidgetManager.updateAppWidget(widgetIds, updatedViews);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top