Skip to content

Instantly share code, notes, and snippets.

@manumaticx
Last active March 16, 2017 14:47
Show Gist options
  • Save manumaticx/6239830 to your computer and use it in GitHub Desktop.
Save manumaticx/6239830 to your computer and use it in GitHub Desktop.
Android background service called from bencoding.AlarmManager BackgroundNotification.js is located under Resources/android/ or app/assets/android (in Alloy) Stackoverflow question: http://stackoverflow.com/questions/18234250/titanium-appcelerator-android-daily-notification-at-some-specified-time
/* locate this file under:
* - Resources/android/
* or
* - app/assets/android/ (when working with Alloy)
*/
var service = Ti.Android.currentService;
var serviceIntent = service.getIntent();
setNotification();
Ti.Android.stopService(serviceIntent);
function setNotification(alarm){
var activity = Ti.Android.currentActivity;
var intent = Ti.Android.createIntent({
action : Ti.Android.ACTION_MAIN,
className : 'com.manumaticx.notificationtest.TestActivity',
flags : Ti.Android.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Ti.Android.FLAG_ACTIVITY_SINGLE_TOP
});
intent.addCategory(Titanium.Android.CATEGORY_LAUNCHER);
var pending = Ti.Android.createPendingIntent({
activity : activity,
intent : intent,
type : Ti.Android.PENDING_INTENT_FOR_ACTIVITY,
flags : Ti.Android.FLAG_ACTIVITY_NO_HISTORY
});
var message = "Time is up!";
var notificationOptions = {
contentIntent : pending,
contentTitle : 'Notification Test',
contentText : message,
tickerText : message,
when : new Date().getTime(),
icon : Ti.App.Android.R.drawable.appicon,
flags : Titanium.Android.FLAG_AUTO_CANCEL | Titanium.Android.FLAG_SHOW_LIGHTS | Titanium.Android.FLAG_INSISTENT,
sound: Ti.Filesystem.getResRawDirectory() + 'sound';
};
var notification = Ti.Android.createNotification(notificationOptions);
Ti.Android.NotificationManager.notify(1, notification);
Ti.Media.vibrate([0,100,100,200,100,100,200,100,100,200]);
}
// Note, that my app-id is: com.manumaticx.notificationtest
var now = new Date();
if (OS_ANDROID){
var _alarmModule = require('bencoding.alarmmanager');
var _alarmManager = _alarmModule.createAlarmManager();
}
_alarmManager.addAlarmService({
service:'com.manumaticx.notificationtest.BackgroundNotificationService',
year: now.getFullYear(),
month: now.getMonth(),
day: now.getDate(),
hour: now.getHours(),
minute: now.getMinutes(),
repeat:'daily'
});
<android>
<manifest android:installLocation="auto" android:versionCode="1" android:versionName="1" package="com.manumaticx.notificationtest" xmlns:android="http://schemas.android.com/apk/res/android">
<supports-screens android:anyDensity="true"/><uses-sdk android:minSdkVersion="10" android:targetSdkVersion="17"/>
<application android:debuggable="false" android:icon="@drawable/appicon" android:label="Test" android:name="TestApplication">
<receiver android:name="bencoding.alarmmanager.AlarmNotificationListener"/>
<receiver android:name="bencoding.alarmmanager.AlarmServiceListener"/>
<activity android:configChanges="keyboardHidden|orientation|screenSize"
android:alwaysRetainTaskState="true"
android:label="Test"
android:name=".TestActivity"
android:theme="@style/Theme.Titanium"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:configChanges="keyboardHidden|orientation|screenSize" android:name="org.appcelerator.titanium.TiActivity"/>
<activity android:configChanges="keyboardHidden|orientation|screenSize" android:name="org.appcelerator.titanium.TiTranslucentActivity" android:theme="@android:style/Theme.Translucent"/>
<activity android:configChanges="keyboardHidden|orientation|screenSize" android:name="org.appcelerator.titanium.TiModalActivity" android:theme="@android:style/Theme.Translucent"/>
<activity android:configChanges="keyboardHidden|orientation|screenSize" android:name="ti.modules.titanium.ui.TiTabActivity"/>
<activity android:name="ti.modules.titanium.ui.android.TiPreferencesActivity"/>
<service android:name="com.manumaticx.notificationtest.BackgroundNotificationService"/>
<service android:exported="false" android:name="org.appcelerator.titanium.analytics.TiAnalyticsService"/>
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
<services>
<service type="interval" url="BackgroundNotification.js"/>
</services>
</android>
@theamith
Copy link

In index.js how you given the service name as
service:'com.manumaticx.notificationtest.BackgroundNotificationService'
from where you got the service name?

@manumaticx
Copy link
Author

When you run your app after adding this part to the tiapp.xml

<services>
    <service type="interval" url="BackgroundNotification.js"/>
</services>

you can find the generated Android Manifest under build/android/AndroidManifest.xml

You'll get the full service name from there. It's basically your app id + the service name.

@theamith
Copy link

my tiapp.ml is

<android xmlns:android="http://schemas.android.com/apk/res/android">
    <tool-api-level>16</tool-api-level>
    <manifest>
        <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="11"/>
    </manifest>
    <services>
        <service  url="EventNotificatoinService.js" type="interval"/>
    </services>
</android>
<modules>
    <module platform="android">facebook</module>
    <module version="0.8">bencoding.alarmmanager</module>
</modules>

After compiling the service is not added in the android manifest under build/android/AndroidManifest.xml. Do you know why does it behaves like that?

@manumaticx
Copy link
Author

Have you tried to perform a full rebuild?
You should find the service withinh the element.
It should look something like
<service android:name="com.yourappid.EventNotificatoinService"/>

@theamith
Copy link

is it possible to restart bencoding alarm service after device reboot??

@fabris71
Copy link

Hi, I tried to use bencoding module whit titanium SDK 3.1.3.GA - Alloy

In my tiapp.xml there is:

bencoding.alarmmanager

and in my alloy.js there is:
var _alarmModule = require('bencoding.alarmmanager');
and other code

When I launch app, my Android Emulator crash with error:
requested modules not found

Do you know why?

Thanks in advance and sorry for my bad english :-)

@manumaticx
Copy link
Author

Have you included the module?
http://docs.appcelerator.com/titanium/latest/#!/guide/Using_Titanium_Modules

Have you compiled the module?

@innokria
Copy link

Hey manu
How to set multiple alarm using this method with different Notification title and how to stop those alarms , since each time you are passing
Ti.Android.NotificationManager.notify(1, notification); where 1 is the alarmId .

Plz suggest

@vikasbarve
Copy link

Hey manu
How to compile the module ?
Please help

@joy-cn
Copy link

joy-cn commented Aug 17, 2015

very cool, have any idea on how to implement my own service using ti module?

@julien9999
Copy link

hi, is this still working today ?
thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment