Last active
March 16, 2017 14:47
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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' | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
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
Hey manu
How to compile the module ?
Please help
very cool, have any idea on how to implement my own service using ti module?
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
Have you included the module?
http://docs.appcelerator.com/titanium/latest/#!/guide/Using_Titanium_Modules
Have you compiled the module?