我正在为 Urban Airship 创建一个 ANE,这是一项用于发送推送通知(以及其他内容)的服务。到目前为止,集成效果很好,但只有在应用程序打开时才有效。退出应用程序时,收到新的推送通知会导致应用程序崩溃:
11-29 01:19:32.448 22340-22340/air.com.example.app E/Urban Airship Autopilot: Unable to takeOff automatically
11-29 01:19:32.496 22340-22440/air.com.example.app E/AndroidRuntime: FATAL EXCEPTION: IntentService[PushService]
Process: air.com.example.app, PID: 22340
java.lang.IllegalStateException: Take off must be called before shared()
at com.urbanairship.UAirship.shared(UAirship.java:147)
at com.urbanairship.BaseIntentService.onHandleIntent(BaseIntentService.java:94)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.os.HandlerThread.run(HandlerThread.java)
通过大量挖掘,我认为问题在于我是从 TakeoffFunction(我的 ANE 中的一个 FREFunction)中调用 UAirship.takeOff(),而不是从应用程序的主要 onCreate 方法中调用它(正如在: UrbanAirship NPE )
这是我的 TakeoffFunction:
public class TakeoffFunction implements FREFunction
{
@Override
public FREObject call(FREContext context, FREObject[] freObjects)
{
Log.d("TakeoffFunction", "Attempting Urban Airship TAKEOFF");
Application app = context.getActivity().getApplication();
Log.d("TakeoffFunction", "app found: " + app);
AirshipConfigOptions options = new AirshipConfigOptions();
options.developmentAppKey = "xxx";
options.developmentAppSecret = "xxx";
options.inProduction = false;
options.gcmSender = "000";
Log.d("TakeoffFunction", "Prepare to Launch...");
UAirship.takeOff(app, options, new UAirship.OnReadyCallback()
{
@Override
public void onAirshipReady(UAirship uAirship)
{
Log.d("TakeoffFunction", "Urban Airship is ready after takeoff");
uAirship.getPushManager().setUserNotificationsEnabled(true);
Log.d("TakeoffFunction", "User notifications have been enabled");
}
});
return null;
}
}
因此,我似乎需要以某种方式从主应用程序的 onCreate 方法中调用 UAirship.takeOff()。然而,这被证明是一个挑战,因为我知道对于 Adobe AIR,有一个 AppEntry 类作为主要的应用程序类,但据我所知,这个类是禁止开发人员修改的。我找到了这个教程:http://blogs.adobe.com/digitalmedia/2011/05/extending-air-for-android/从 2011 年开始,在正式支持原生扩展之前。在那里我看到他们能够覆盖和扩展 onCreate() 方法,但我不知道我将如何使用这个 native 扩展来做同样的事情。
我想知道是否可以扩展 AppEntry 的 onCreate 方法或将 AIR 指向一个不同的 AppEntry 类,覆盖原来的类。
最佳答案
在创建所有服务、接收器或 Activity 之前,通常会看到库希望让应用程序在应用程序的 onCreate 中初始化其模块。基本上是所有应用程序的唯一真正入口点。
对于像 Adobe 这样难以在应用程序文件中调用 takeoff 的框架,我们添加了另一种使用 Autopilot 调用 takeoff 的方法。 .
例子:
public class AirAutopilot extends Autopilot {
@Override
public AirshipConfigOptions createAirshipConfigOptions(Context context) {
AirshipConfigOptions options = new AirshipConfigOptions();
options.developmentAppKey = "xxx";
options.developmentAppSecret = "xxx";
options.inProduction = false;
options.gcmSender = "000";
return options;
}
@Override
public void onAirshipReady(UAirship airship) {
Log.d("TakeoffFunction", "Urban Airship is ready after takeoff");
airship.getPushManager().setUserNotificationsEnabled(true);
Log.d("TakeoffFunction", "User notifications have been enabled");
}
}
然后将自动驾驶仪类添加到应用程序 block 中的 list (不确定 Adobe Air 的做法):
<meta-data android:name="com.urbanairship.autopilot" android:value="com.example.AirAutopilot" />
然后在您的插件中,确保在访问 UAirship 实例之前调用自动驾驶仪:
Autopilot.automaticTakeOff(context.getActivity().getApplication());
关于android - 从 Adobe AIR 扩展连接到主应用程序的 onCreate 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33980152/