gitn00b1337 / expo-widgets

Bringing widget functionality to expo!
148 stars 12 forks source link

widget does not appear on android #19

Closed Brunowilliang closed 5 months ago

Brunowilliang commented 5 months ago

I followed the step-by-step guide to add the widget to my app. I can build it with:

npx expo prebuild --clean
npx expo run:android

and the app works fine, but the widget doesn't appear. The widget only becomes visible when I add the following configuration to the AndroidManifest.xml:

<receiver
    android:name=".SampleWidget"
    android:label="@string/app_widget_description"
    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/sample_widget_info" />
</receiver>
gitn00b1337 commented 5 months ago

Hi,

Just to confirm, have you opened the app before trying to add a widget? If so, try with v 0.6.20.

If that works, then I'll investigate further. If not, please check you haven't missed something and importantly reduce your widget to the bare minimum to check there isn't a runtime error within your kotlin

Brunowilliang commented 5 months ago

Hey @gitn00b1337, I tested it on versions 0.6.20 and 0.7.0 the same problem occurs...

I just took the widgets from the example project, I didn't change much.

and followed the steps to set it up:

[
  "@bittingz/expo-widgets",
  {
    android: {
        src: "./widgets/android",
        resourceName: "@xml/my_widget_info",
    }                      
  }
],

and I added the name of my package along with the '.R' because it was giving me an error.

package ie.idonate.com

import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.Context
import android.widget.RemoteViews
import android.content.SharedPreferences
import ie.idonate.com.R

/**
 * Implementation of App Widget functionality.
 */
class SampleWidget : AppWidgetProvider() {
    override fun onUpdate(
        context: Context,
        appWidgetManager: AppWidgetManager,
        appWidgetIds: IntArray
    ) {
        // There may be multiple widgets active, so update all of them
        for (appWidgetId in appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId)
        }
    }

    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
    }
}

internal fun updateAppWidget(
    context: Context,
    appWidgetManager: AppWidgetManager,
    appWidgetId: Int
) {
    val data = context.getSharedPreferences(context.packageName + ".widgetdata", Context.MODE_PRIVATE).getString("widgetdata", "{}")

    val views = RemoteViews(context.packageName, R.layout.sample_widget)
    views.setTextViewText(R.id.appwidget_text, data)

    // Instruct the widget manager to update the widget
    appWidgetManager.updateAppWidget(appWidgetId, views)
}
gitn00b1337 commented 5 months ago

Ok, does ie.idonate.com match your package name for app.json (android.package)?

Also, I think the example above might be missing this:

[
  "@bittingz/expo-widgets",
  {
   "android": {
            "src": "./widgets/android",
            "widgets": [
              {
                "name": "SampleWidget",
                "resourceName": "@xml/sample_widget_info"
              }
            ]
          }                  
  }
],
gitn00b1337 commented 5 months ago

I've updated the readme; I added multiple widget support a few months ago and clearly forgot the docs

gitn00b1337 commented 5 months ago

I've updated the example project with more details. You can pull down and follow the steps in App.tsx to get a working example