Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Android: Notification creation

using the latest commits on the master branch, the NotificationManager module is pretty easy to use, however, with the use of Intents, it gets slightly complicated.

Anyone interested in sharing how they may get notifications to work… sample code provided..

var win = Titanium.UI.createWindow({  
    title: 'Test',
    backgroundColor: '#fff'
});

win.open();

var note = Ti.Android.NotificationManager.createNotification({
    tickerText: 'This is a notification',
    number: 1,
    contentTitle: 'Notification',
    contentText: 'This is a notification, sucka!',
    flags: Ti.Android.NotificationManager.FLAG_ONGOING_EVENT,
    ///contentIntent: ???, says this is needed, however, nothing documented on how to create the Intent for this
});

Ti.Android.NotificationManager.notify(1, note);
— asked October 21st 2010 by Andrew Heebner
  • android
  • intent
  • manager
  • notification
0 Comments

5 Answers

  • Accepted Answer

    You're missing the activity. We're in between tooling and injecting Ti.Android.currentActivity into the script. Check out this script http://github.com/donthorp/androidng/blob/master/assets/Resources/android-activity-notification.js

    — answered October 22nd 2010 by Don Thorp
    permalink
    0 Comments
  • These APIs are very thin wrappers around the Android API. You'll need to figure out what type of action you want to accomplish when the notification is selected.

    — answered October 22nd 2010 by Don Thorp
    permalink
    1 Comment
    • Sounds good, I'm following some of the chatter between yourself and Jeff about intents and background services. I'm slowly grasping the concept now. This is the one thing that restricted me in Java development. I'll try something simple, such as dialing a phone number for now, but last time i tried, i was being returned an odd error with the Intent creation.

      — commented October 22nd 2010 by Andrew Heebner
  • Here's an updated example, with the intent passed through:

    var win = Titanium.UI.createWindow({  
        title: 'Test',
        backgroundColor: '#fff'
    });
    
    win.open();
    
    var intent = Ti.Android.createIntent({
        action: Ti.Android.ACTION_DIAL,
        data: 'tel:123'
    })
    
    var note = Ti.Android.NotificationManager.createNotification({
        tickerText: 'This is a notification',
        number: 1,
        contentTitle: 'Notification',
        contentText: 'This is a notification, sucka!',
        contentIntent: intent
    });
    
    Ti.Android.NotificationManager.notify(1, note);
    

    When launching on device, i get the error: Wrapped java.lang.ClassCastException: ti.modules.titanium.android.IntentProxy (app://app.js#7)

    Everything appears correct to me, unless I can somehow otherwise be corrected…

    — answered October 22nd 2010 by Andrew Heebner
    permalink
    0 Comments
  • hello,
    anyone can help,
    (I already have notice but no sound)
    can I have a sound?

    is it possible?

    (or vibration)

    — answered January 31st 2012 by André Silva
    permalink
    1 Comment
    • with this:

      var notification = Ti.Android.createNotification({
          default:Ti.Android.DEFAULT_ALL,
      });
      

      — commented December 16th 2014 by andres guerrero
  • im having troubles whit a intent dont launch activity, im little newbie.
    here's my code:
    app.js

    var activity = Ti.Android.currentActivity;
                var intent     = Ti.Android.createIntent({
                    action: Ti.Android.ACTION_VIEW,
                    className : 'com.galgomedia.planvitalapp.inboxActivity',
                    packageName : 'com.galgomedia.planvitalapp',
                    url: 'messageIncoming.js',
                    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,
                 updateCurrentIntent:true
                });
                var notification = Ti.Android.createNotification({
                    contentIntent : pending,
                    contentTitle :L('titleHeaderMessage'),
                    contentText : data.title,
                    tickerText :  L('tickerIncomingMessage'),
                    icon:Ti.App.Android.R.drawable.appicon,
                    when:0,
                    default:Ti.Android.DEFAULT_ALL,
                    flags : Titanium.Android.ACTION_DEFAULT | Titanium.Android.FLAG_AUTO_CANCEL | Titanium.Android.FLAG_SHOW_LIGHTS
                });
                Ti.Android.NotificationManager.notify(1, notification);
    

    tiapp.xml:

    <android xmlns:android="http://schemas.android.com/apk/res/android">
            <activities>
                <activity 
                    url="messageIncoming.js" 
                    android:name=".inboxActivity"
                    android:label="inboxActivity">
                    <intent-filter>
                        <action android:name="android.intent.action.VIEW"/>
                        <category android:name="android.intent.category.LAUNCHER"/>
                    </intent-filter> 
                </activity>
            </activities>
            <services>
                <!-- <service type="interval" url="messageIncoming.js"/> -->
            </services>
            <manifest>
                <uses-permission android:name="android.permission.VIBRATE"/>
                <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
                <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
                <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
                <uses-permission android:name="android.permission.INTERNET"/>
                <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
                <uses-permission android:name="android.permission.WAKE_LOCK"/>
                <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
                <permission android:name="com.galgomedia.planvitalapp.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
                <uses-permission android:name="com.galgomedia.planvitalapp.permission.C2D_MESSAGE"/>
                <application android:allowBackup="true" android:screenOrientation="portrait" android:theme="@style/Theme.NoActionBar">
                    <activity android:name="com.arellomobile.android.push.PushWebview"/>
                    <activity android:name="com.arellomobile.android.push.MessageActivity"/>
                    <activity android:name="com.arellomobile.android.push.PushHandlerActivity"/>
                    <receiver android:name="com.arellomobile.push.IntentReceiver"/>
                    <receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:per
    mission="com.google.android.c2dm.permission.SEND">
                        <intent-filter>
                        <!-- Receives the actual messages. -->
                            <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
                            <!-- Receives the registration id. -->
                            <action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
                            <category android:name="com.galgomedia.planvitalapp"/>
                        </intent-filter>
                    </receiver>
                    <service android:name="com.arellomobile.android.push.PushGCMIntentService"/>
                </application>
            </manifest>
        </android>
    

    and the file messageIncoming.js is placed in the same level than app.js
    incomingMessage.js:

    var service = Ti.Android.currentService;
    var intent = service.getIntent();
    var viewInbox = require("activityInbox");
    var inbox = new viewInbox();
    inbox.winForm.open({animation:true});
    inbox.load();
    inbox.checkNews();
    

    pls help me
    im little stucked with this

    — answered December 16th 2014 by andres guerrero
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.