Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appcelerator Titanium LocalNotification for Android

IS there any LocalNotification available in Android.(some thing similar to Titanium.App.iOS.scheduleLocalNotification)

I need some simple notification(like alert) for Android Platform, even if my application closed. Is it possible in Titanium?

like image 284
Karthi Ponnusamy Avatar asked Dec 06 '11 18:12

Karthi Ponnusamy


2 Answers

In android you can also fire notification but it will not be shown as alert box. Instead it will be shown in notification area. Here is code snippet to to fire notification in android.

alarmTimeIntent = Ti.Android.createIntent({
                className: 'org.appcelerator.titanium.TiActivity',
                packageName: 'package name here',
                flags: Titanium.Android.FLAG_ACTIVITY_CLEAR_TOP | Titanium.Android.FLAG_ACTIVITY_SINGLE_TOP 
            });

            var alarmTimePendingIntent = Ti.Android.createPendingIntent({
                activity: Ti.Android.currentActivity,
                intent: alarmTimeIntent,
                type: Ti.Android.PENDING_INTENT_FOR_ACTIVITY,
                flags: Titanium.Android.FLAG_CANCEL_CURRENT
            });

            var alarmTimeNotification = Titanium.Android.createNotification({
                contentIntent: alarmTimePendingIntent,
                contentTitle: 'Content Title Here',
                tickerText: 'Ticker text here',
                defaults:Titanium.Android.NotificationManager.DEFAULT_SOUND,
                when: new Date()
            });

            Ti.Android.NotificationManager.notify(1, alarmTimeNotification);
like image 63
The Zero Avatar answered Sep 27 '22 23:09

The Zero


Use Ben Bahrenburg's Bencoding AlarmManager: https://github.com/benbahrenburg/benCoding.AlarmManager

This module provides what you need. It's really easy - just set repeat to daily when sheduling a Notification or Service.

i am posting the code to create daily notification

app.js

//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',//your service name as in AndroidManifest.xml
        hour : new Date().getHours(),
        repeat : 'daily'
    });
}

dailyEventNotificatoin.js // notification alarm service file

/* 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]);
}

tiapp.xml

<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>
like image 25
Amith Avatar answered Sep 28 '22 00:09

Amith