Reputation: 1
I'm trying to create an AIR native extension for sending scheduled notifications, I have the following code:
package com.ane.notification.functions;
public class ScheduledNotificationFunction implements FREFunction {
Context androidActivity;
Context androidContext;
@Override
public FREObject call(FREContext context, FREObject[] args) {
androidActivity = context.getActivity();
androidContext = androidActivity.getApplicationContext();
AlarmManager alarmManager = (AlarmManager) androidContext.getSystemService(Context.ALARM_SERVICE);
int id = (int) System.currentTimeMillis();
Intent intent = new Intent(androidContext, SendNotification.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(androidContext, id, intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, (System.currentTimeMillis()+30000), pendingIntent);
return null;
}
}
Class SendNotification
package com.ane.notification.functions;
public class SendNotification extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Send Notification Code
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ane.notification"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />
<application android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:theme="@style/AppTheme">
<receiver android:name=".functions.SendNotification"/>
</application>
</manifest>
The problem is that not called SendNotification once marks the definite time to send the notification. I think the ScheduledNotificationFunction code is fine, since I have put in a try / catch and does not generate exceptions, so I assume the alert is register in the system.
I'm not very familiar with programming in android, so anything missing on this wrong or let me know, Thanks.
Upvotes: 0
Views: 1044
Reputation: 3871
I suggest you use actions to filter these sort of requests so you should have something that looks more like this:
long fireTime = SystemClock.elapsedRealtime() + 1000;
Intent intent = new Intent( "com.ane.notification.functions.SOME_ACTION" );
PendingIntent delayPendingIntent = PendingIntent.getBroadcast(appContext, 0, intent, 0);
AlarmManager am = (AlarmManager)appContext.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.ELAPSED_REALTIME, fireTime, delayPendingIntent);
Then you need to add that manifest addition to your AIR application descriptor not the Android Manifest. Also you need to use the full class package in the manifest:
<android>
<manifestAdditions><![CDATA[
<manifest android:installLocation="auto" >
<uses-permission android:name="android.permission.INTERNET"/>
<application>
<receiver android:name="com.ane.notification.functions.SendNotification">
<intent-filter>
<action android:name="com.ane.notification.functions.SOME_ACTION"/>
</intent-filter>
</receiver>
</application>
</manifest>
]]></manifestAdditions>
</android>
You must register the receiver in the manifest for the system to call it when the application isn't running. You can do it in code if you just want a receiver while your application is running and not deal with the manifest additions then.
Note: If your application has been stopped before the alarm fires your FREContext won't be initialised even though you'll be able to run code in your SendNotification receiver.
Upvotes: 2