Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

home_widget interactive callback not triggered #338

Open
ABausG opened this issue Mar 6, 2025 · 1 comment
Open

home_widget interactive callback not triggered #338

ABausG opened this issue Mar 6, 2025 · 1 comment

Comments

@ABausG
Copy link
Owner

ABausG commented Mar 6, 2025

Discussed in #323

Originally posted by avorio-dev January 4, 2025
Hello,
I'm having an issue with the home_widget package, specifically with the interactiveCallback function. Once the refresh icon is pressed in my widget, I want it to update the data using the appropriate method in my Flutter class. I don't understand why the event is not triggering. Please, could you provide any suggestions?

Below, there is my code. What I expect is the trigger of backgroundCallback: Anyway, the normal update of elements in my widget is correct. Only the background callback not working

Manifest.xml

<receiver
    android:name=".MyAppHomeWidget"
    android:exported="true">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>

    <meta-data
        android:name="android.appwidget.provider"
        android:resource="@xml/my_home_widget_info" />
</receiver>


<!-- Used for Background Work -->
<receiver
    android:name="es.antonborri.home_widget.HomeWidgetBackgroundReceiver"
    android:exported="true">
    <intent-filter>
        <action android:name="es.antonborri.home_widget.action.BACKGROUND" />
    </intent-filter>
</receiver>
<service
    android:name="es.antonborri.home_widget.HomeWidgetBackgroundService"
    android:exported="true"
    android:permission="android.permission.BIND_JOB_SERVICE" />

Kotlin

class MyAppHomeWidget : HomeWidgetProvider() {
    override fun onReceive(context: Context?, intent: Intent?) {
        super.onReceive(context, intent)
    }

    override fun onUpdate(
        context: Context,
        appWidgetManager: AppWidgetManager,
        appWidgetIds: IntArray,
        widgetData: SharedPreferences
    ) {

        appWidgetIds.forEach { appWidgetId ->
            val views = RemoteViews(context.packageName, R.layout.my_app_home_widget).apply {

                // Open App on Widget Click
                val openAppOnClickIntent =
                    HomeWidgetLaunchIntent.getActivity(context, MainActivity::class.java)
                setOnClickPendingIntent(R.id.widget_container, openAppOnClickIntent)

                // Swap Title Text by calling Dart Code in the Background
                val refreshDataIntent =
                    HomeWidgetBackgroundIntent.getBroadcast(
                        context, Uri.parse("myHomeWidget://updateLocationAndForecast")
                    )
                setOnClickPendingIntent(R.id.refreshDataIcon, refreshDataIntent)

                // Update widget data
                setTextViewText(
                    R.id.widgetLocation,
                    widgetData.getString("widgetLocation", "Somewhere")
                )
                setTextViewText(
                    R.id.widgetWeatherCondition,
                    widgetData.getString("widgetWeatherCondition", "Unknown")
                )

                setTextViewText(
                    R.id.widgetTemperature,
                    widgetData.getString("widgetTemperature", "N/A")
                )
                setTextViewText(R.id.widgetHumidity, widgetData.getString("widgetHumidity", "N/A"))
                setTextViewText(R.id.widgetRainProb, widgetData.getString("widgetRainProb", "N/A"))
                setTextViewText(R.id.widgetWind, widgetData.getString("widgetWind", "N/A"))

                setTextViewText(
                    R.id.widgetLastUpdateDate,
                    widgetData.getString("widgetLastUpdateDate", "N/A") ?: "No Title Set"
                )

                //setTextViewText(R.id.widgetLastUpdateDate, widgetData.getString("widgetLastUpdateDate", "Unknown"))
            }

            appWidgetManager.updateAppWidget(appWidgetId, views)
        }
    }

    override fun onEnabled(context: Context) {
        // Enter relevant functionality for when the first widget is created
    }

    override fun onDisabled(context: Context) {
        // Enter relevant functionality for when the last widget is disabled
    }
}

Dart

class HomeWidgetController {
  static const String _classname = 'HomeWidgetController';

  /// Called when Doing Background Work initiated from Widget
  @pragma("vm:entry-point")
  static Future<void> backgroundCallback(Uri? data) async {
    AppLogger.info(_classname, 'App Widget call with URI: $data');
    if (data?.host == 'updateLocationAndForecast') {
   
    
    }
  }

  static Future<void> initialize() async {
    HomeWidget.setAppGroupId(GlobalVar.widgetGroupId);
    await HomeWidget.registerInteractivityCallback(backgroundCallback);

  }
}

the method initialize is called in main.dart as follow:

WidgetsFlutterBinding.ensureInitialized();
await HomeWidgetController.initialize();
```</div>
@ABausG
Copy link
Owner Author

ABausG commented Mar 6, 2025

Hey @avorio-dev and @Atikur-Rahman-Sabuj moved the discussion in a separate issue.

Can you please provide:

  • Flutter Version
  • Android Version
  • Confirm which Version of home_widget you are using. Please try with the latest version (0.7.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant