Skip to content

Instantly share code, notes, and snippets.

@theamith
Forked from manumaticx/BackgroundNotification.js
Last active December 21, 2015 04:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save theamith/6248106 to your computer and use it in GitHub Desktop.
Save theamith/6248106 to your computer and use it in GitHub Desktop.
Android background service called from bencoding.AlarmManager BackgroundNotification.js for scheduling daily notification
//Import bencoding alarmmanager module into our Titanium App
var alarmModule = require('bencoding.alarmmanager');
var alarmManager = alarmModule.createAlarmManager();
var isRunning = Ti.App.Properties.getBool("service_running", false);//get service running bool status
if (isRunning) {
Ti.API.info('service is running');
} else {
Ti.API.info('service is not running');
alarmManager.addAlarmService({
service : 'com.mkamithkumar.whatstoday.DailyEventNotificatoinService',
hour : "08",
repeat : 'daily'
});
}
/* 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.mkamithkumar.whatstoday.WhatsTodayActivity',
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 : 'Whats Today - Featured Event',
contentText : 'Tap to see todays featured event',
tickerText : 'Whats Today Event Notification!',
//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 : Titanium.Android.NotificationManager.DEFAULT_SOUND
};
var notification = Ti.Android.createNotification(notificationOptions);
Ti.Android.NotificationManager.notify(1, notification);
Ti.App.Properties.setBool("service_running", true);
Ti.Media.vibrate([0, 100, 100, 200, 100, 100, 200, 100, 100, 200]);
}
<android xmlns:android="http://schemas.android.com/apk/res/android">
<tool-api-level>16</tool-api-level>
<manifest android:installLocation="auto" android:versionCode="1" android:versionName="1" package="com.mkamithkumar.whatstoday" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="11"/>
<supports-screens android:anyDensity="true"/>
<application android:debuggable="false" android:icon="@drawable/appicon" android:label="Whats Today" android:name="WhatsTodayApplication" android:theme="@android:style/Theme.DeviceDefault.Light">
<receiver android:name="bencoding.alarmmanager.AlarmNotificationListener"/>
<receiver android:name="bencoding.alarmmanager.AlarmServiceListener"/>
<activity android:configChanges="keyboardHidden|orientation|screenSize"
android:alwaysRetainTaskState="true"
android:label="Whats Today"
android:name=".WhatsTodayActivity"
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:exported="false" android:name="org.appcelerator.titanium.analytics.TiAnalyticsService"/>
</application>
<uses-permission android:name="android.permission.VIBRATE"/>
<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="dailyEventNotificatoin.js"/>
</services>
</android>
@Kalpb
Copy link

Kalpb commented Oct 21, 2013

Hi itsamiths,

how did you got the service name in app.js?
"service : 'com.mkamithkumar.whatstoday.DailyEventNotificatoinService',"

and can we put any name in "android:name=".WhatsTodayActivity"" (from tiapp.xml) and "className : 'com.mkamithkumar.whatstoday.WhatsTodayActivity'," (from dailyEventNotificatoin.js) ?

@innokria
Copy link

this dosen't works if you kill the app ?? am i right to say that?

@MahmoudElmoghazy
Copy link

can i make more than on service

@hirenpatel20283
Copy link

I want to register my current location every one hour in local database even app is in foreground,background or totally close.Can you please suggest best way to do this.Is alarm manager is useful for achieve such functionality?

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