Appcelerator Titanium LocalNotification for Android
Asked Answered
W

2

5

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?

Warfourd answered 6/12, 2011 at 18:7 Comment(0)
M
8

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);
Mancino answered 7/12, 2011 at 3:56 Comment(6)
whether it will work even if I closed my application?, and what should I use here (packageName: 'package name here')Warfourd
actually i written com.*.* my app id, but i don't think it will effect. It will work without that too i think but still just write you app id or anything related to com.*.* .... And yah it will work but you have use background service. Because in android it will not fire automatically like iphone do. you have to fire manually.Mancino
Last line .notify will fire notification.Mancino
Thanks ..I will check this ...Do you have any idea how to run background service in Titanium for Android Platform?Warfourd
you have to create one service file in resources/android/ folder. And make and entry in to tiapp.xml than you can call that service from your app.Mancino
@TheZero Would it work even if the app is removed from background? As per appcelerator docs background service is stopped when the app is removed from background. Do you know any native module or other solution for that case?Pomona
I
2

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>
Invalidate answered 16/8, 2013 at 7:50 Comment(1)
benCoding.AlarmManager is really cool but can you open your app on clicking the notification? What if I must need this feature?Pomona

© 2022 - 2024 — McMap. All rights reserved.