Titanium Community Questions & Answer Archive

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

C2dm in titanium

Hi!!! Anyone knows about new c2dm for Android in Titanium? I see Push for iPhone but nothing about c2dm.

Thanks¡¡¡

— asked June 25th 2010 by Ivan Hernandez
  • android
  • c2dm
  • mobile
0 Comments

13 Answers

  • Hi,

    as I spent a couple of hours to get the module working, I'm going to try to post here an exhaustive installation guide :

    • Make sure you have an account configured in your device (event if it's a simulator).
    • Uncompress the IGx89-titanium-c2dm-3b05b8a.zip archive
    • from the resulting directory, copy dist/c2dm.jar and manifest to a folder named modules/android/com.findlaw.titanium.c2dm
    • Copy timodule.xml at your project root.
    • Your project structure now looks like :
    YourProject/
    ...
    ??? i18n
    ?   ??? ...
    ...
    ??? manifest
    ??? modules
    ?   ??? android
    ?       ??? com.findlaw.titanium.c2dm
    ?           ??? c2dm.jar
    ?           ??? manifest
    ...
    ??? tiapp.xml
    ??? timodule.xml
    
    • In timodule.xml replace all occurences of com.findlaw.titanium.c2dm with your project id (for instance com.myapp).
    • In tiapp.xml add the following :
    <android xmlns:android="http://schemas.android.com/apk/res/android">
            <manifest>
                <permission android:name="[yourprojectid].permission.C2D_MESSAGE" android:protectionLevel="signature"/>
                <uses-permission android:name="[yourprojectid].permission.C2D_MESSAGE"/>
    
                <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
                <uses-permission android:name="android.permission.WAKE_LOCK"/>
                <application>
                    <service android:name="com.findlaw.titanium.c2dm.C2DMReceiver"/>
                    <receiver
                        android:name="com.google.android.c2dm.C2DMBroadcastReceiver" 
                            android:permission="com.google.android.c2dm.permission.SEND">
                        <intent-filter>
                            <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
                            <category android:name="[yourprojectid]"/>
                        </intent-filter>
                        <intent-filter>
                            <action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
                            <category android:name="[yourprojectid]"/>
                        </intent-filter>
                    </receiver>
                </application>
            </manifest>
        </android>
        <modules>
            <module platform="android" version="0.1">com.findlaw.titanium.c2dm</module>
        </modules>
    

    It's important to use the project id and not another package name : otherwise the module will not be able to register.

    • In your app.js you can use the following as the base (this code has been taken from the example directory of the module)
    var senderId = 'yourid@gmail.com';
    
    var c2dm = require('com.findlaw.titanium.c2dm');
    Ti.API.info("module is => " + c2dm);
    
    Ti.API.info('Registering...');
    c2dm.register(senderId, {
        success:function(e)    {
            Ti.API.info('JS registration success event: ' + e.registrationId);
            // send the registration is to your server
        },
        error:function(e) {
            Ti.API.error("Error during registration: "+e.error);
    
            var message;
            if(e.error == "ACCOUNT_MISSING") {
                message = "No Google account found; you'll need to add one (in Settings/Accounts) in order to activate notifications";
            } else {
                message = "Error during registration: "+e.error
            }
    
            Titanium.UI.createAlertDialog({
                title: 'Push Notification Setup',
                message: message,
                buttonNames: ['OK']
            }).show();
        },
        callback:function(e) // called when a push notification is received
        {
            Ti.API.info('JS message event: ' + JSON.stringify(e.data));
    
            var intent = Ti.Android.createIntent({
                action: Ti.Android.ACTION_MAIN,
                flags: Ti.Android.FLAG_ACTIVITY_NEW_TASK | Ti.Android.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED,
                className: '[yourprojectid][yourProject]Activity',
                packageName: '[yourprojectid]'
            });
            intent.addCategory(Ti.Android.CATEGORY_LAUNCHER);
    
            // This is fairly static: Not much need to be altered here
            var pending = Ti.Android.createPendingIntent({
                activity: Ti.Android.currentActivity,
                intent: intent,
                type: Ti.Android.PENDING_INTENT_FOR_ACTIVITY,
            });
    
            var notification = Ti.Android.createNotification({
                contentIntent: pending,
                contentTitle: 'New message',
                contentText: e.data.message,
                tickerText: "New message"
            });
    
            Ti.Android.NotificationManager.notify(1, notification);
        }
    });
    

    You can find your main activity name by using a ctrl+f -> Activity in the Titanium logs.

    After that it should be working.

    — answered October 18th 2011 by Raphaël Lemaire
    permalink
    13 Comments
    • Sorry for ??? in the directory structure, it looked nice on the preview.

      It's the result of a tree command call on the root project directory.

      — commented October 18th 2011 by Raphaël Lemaire
    • Link to the module: https://github.com/IGx89/titanium-c2dm

      — commented October 20th 2011 by Matthew Lieder
    • I've got this implemented, but my app never gets registered. I've taken the exact code from the example in the github repo, dropped it in my app, changing only the app id. I've copied all the relevant manifest settings. Nothing ever happens. I'm testing on a Xoom tablet, if that matters.

      Are push notifications only a pipe dream on Android at this time? It seems very shaky, at best.

      — commented October 27th 2011 by Dan Murrell
    • You should also include

      <uses-permission android:name="android.permission.USE_CREDENTIALS" />
      <uses-permission android:name="android.permission.WAKE_LOCK" />
      

      to allow the app to correctly use the user's credentials.

      — commented November 10th 2011 by sebastien requiem
    • hi
      I am not getting third step ..

      "from the resulting directory, copy dist/c2dm.jar and manifest to a folder named modules/android/com.findlaw.titanium.c2dm"…

      should I create folder structure like modules/android/com.findlaw.titanium.c2dm please describe this…

      thanks

      — commented February 28th 2012 by veibhav d
    • Hello friends I am getting error below, after followed above procedure…

      [ERROR] The 'apiversion' for 'com.findlaw.titanium.c2dm' in the module manifest is not a valid value. Please use a version of the module that has an 'apiversion' value of 2 or greater set in it's manifest file

      — commented February 29th 2012 by veibhav d
    • hai how to create com.findlaw.titanium.c2dm package in titanium and i followed above steps but error like "Could not found requested module:com.findlaw.titanium.c2dms"

      — commented March 8th 2012 by sugan s
    • Hello Friends I am also getting same error.

      The 'apiversion' for 'com.findlaw.titanium.c2dm' in the module manifest is not a valid value. Please use a version of the module that has an 'apiversion' value of 2 or greater set in it's manifest file

      Please suggest.
      Thanks before hand.

      — commented April 12th 2012 by chandra prakash dadhich
    • I successfully built my own module by following these instructions:

      http://technoblog-bhushan.blogspot.com/2012/03/looking-at-all-possible-solution-for.html

      — commented April 26th 2012 by Jon Dal
    • Is this module works when I close my APP, means I still get a push notification when I close my App???

      — commented May 14th 2012 by Ajeet pratap Maurya
    • Rather use the built in Titanium cloud push module that comes with Titanium Studio version 2.

      — commented May 14th 2012 by Zane Wright
    • can anyone help me with this….I am getting this error .

      symbol  : class C2dmBootstrap
      location: package com.findlaw.titanium.c2dm
              runtime.addExternalModule("com.findlaw.titanium.c2dm", com.findlaw.titanium.c2dm.C2dmBootstrap.class);
      

      — commented May 15th 2012 by Ajeet pratap Maurya
    • I have a similar problem!

      — commented June 26th 2012 by Mike Merritt-Holmes
  • Any news on this?
    Thanks

    — answered September 8th 2010 by Dan Tamas
    permalink
    0 Comments
  • Ivan/Lorenzo, I saw you had some luck creating a C2DM module. I've very interested in using your code; are you able to share it?

    — answered October 6th 2010 by Matthew Lieder
    permalink
    2 Comments
    • FYI, I created one myself earlier this year: https://github.com/IGx89/titanium-c2dm

      That module is what Raphaël is referring to below.

      — commented October 20th 2011 by Matthew Lieder
    • Or above :p

      — commented October 20th 2011 by Matthew Lieder
  • Hello, I made the module work with 1.8.0.1. Github: titanium-c2dm

    — answered January 9th 2012 by Joonas Ruotsalainen
    permalink
    5 Comments
    • Hi, I used your library (thanks a lot by the way) to compile against the Titanium 1.8.1 SDK. Everything works but when receiving a message I get the following error in ddms:

      02-20 12:44:25.569: W/ActivityManager(18777): Unable to start service Intent { act=com.google.android.c2dm.intent.RECEIVE cat=[de.thethinklink.mediankarriere] cmp=de.thethinklink.mediankarriere/com.findlaw.c2dm.C2DMReceiver (has extras) }: not found
      

      This is my tiapp.xml (just the corresponding part):

      <permission android:name="de.thethinklink.mediankarriere.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
        <uses-permission android:name="de.thethinklink.mediankarriere.permission.C2D_MESSAGE"/>
        <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
        <uses-permission android:name="android.permission.WAKE_LOCK"/>
         <application>
           <service android:name=".C2DMReceiver"/>
           <receiver android:name="com.google.android.c2dm.C2DMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND">
             <intent-filter>
               <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
               <category android:name="de.thethinklink.mediankarriere"/>
             </intent-filter>
             <intent-filter>
               <action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
               <category android:name="de.thethinklink.mediankarriere"/>
             </intent-filter>
           </receiver>
        </application>
      

      Any idea what happens here and on how to fix this?

      Thanks in advance.

      — commented February 20th 2012 by Robert Schmidt
    • I have same problem. Could you get any solution ?

      — commented February 24th 2012 by Vijay Singh
    • See my post here:

      http://developer.appcelerator.com/question/131680/sdk-181-android-modules-problem

      That was the only way I could get it running.

      — commented February 24th 2012 by Robert Schmidt
    • Can you please put on Github a built version for 1.8.1 as I am not familiar with building modules. Thanks!

      — commented February 29th 2012 by Zane Wright
    • Hello! Does anyone have the compiled version of the module that works with 1.8.1? I'd love if someone could publish it :)

      — commented March 7th 2012 by Anton Gildebrand
  • Applied for an invite - but still waiting to get it from Google. I have a module waiting for Android but can't test it until I get the invite.

    — answered June 26th 2010 by David Ashwood
    permalink
    0 Comments
  • I'm having an issue here guys. Can anyone help? In the 3 callback functions included with c2dm.register() I simply logged a foo to find out if the function was being triggered.

    Turns out that nothing is being logged, so none of the functions are being triggered.

    All I see in the log related to the app is:

    I/TiAPI   (14155): (kroll$1: app://app.js) [97,492] module is => [Ti.C2dm]
    I/TiAPI   (14155): (kroll$1: app://app.js) [0,492] Registering...
    D/C2dmModule(14155): (kroll$1: app://app.js) [0,492] register called
    D/C2dmModule(14155): (kroll$1: app://app.js) [1,493] get registrationId property
    I/TiRootActivity(14155): (main) [0,0] checkpoint, on root activity resume. context = org.appcelerator.titanium.TiContext@47dbfc58
    W/ActivityManager( 2537): Trying to launch com.myapp.push/org.appcelerator.titanium.TiActivity
    W/TiAnalyticsSvc(14155): (Thread-12) [57,57] Analytics Service Started
    W/KeyboardStateImpl( 2622): EditorInfo provided is null or not valid, defaulting to dumb mode!
    I/ActivityManager( 2537): Displayed activity com.myapp.push/org.appcelerator.titanium.TiActivity: 209 ms (total 2284 ms)
    I/TiAnalyticsSvc(14155): (Thread-12) [235,292] Sending 1 analytics events.
    
    — answered November 3rd 2011 by Danny Migliorisi
    permalink
    0 Comments
  • I followed the steps, but keep getting the error:

    Wrapped java.lang.Exception: Module file app://com.findlaw.titanium.c2dm.js does not exist (file:///android_assets/Resources/includes/c2dm.js#3)

    any idea what I am doing wrong?

    — answered December 3rd 2011 by Arjan Brinkman
    permalink
    3 Comments
    • Arjan, are you sure you replaced all instances of [your project id] and also the 'com.findlaw.titanium.c2dm'-entries in the file tiapp.xml or your custom AndroidManifest.xml ?

      I mention this because in the example above (the tiapp.xml sample-code) has actually -next to loads of [your project id]-find-replace- also this line:

      <service android:name="com.findlaw.titanium.c2dm.C2DMReceiver"/>
      

      which should be replaced by your app id? Or did you already find the solution, if so please add it here for future visitors!

      — commented December 21st 2011 by Roeland P
    • in tiapp.xml I had to define <property name="ti.android.runtime">rhino</property> in the root. It seems there is a problem with V8 js engine in 1.8.2 and we need to swich to rhino engine.

      — commented April 12th 2012 by Danut Lipsa
    • i wanna know if we can change the module id from com.findlaw.titanium.c2dm.C2DMReceiver to my customized id.. if i do the registration intent is not starting…

      — commented June 11th 2012 by Rudhra Priya
  • Titanium 1.8 broke this, but it works using 1.7.5. I suspect that if you can build the module yourself, you can get around the break from 1.8.

    — answered January 5th 2012 by Tim Dement
    permalink
    0 Comments
  • Hi Joonas,

    I finally got the titanium-c2dm module running but the register function seems not work. I neither get error or success. I have reg as c2dm sender by google. Please help!

    — answered January 11th 2012 by Åse Dittlau
    permalink
    6 Comments
    • Hi

      Did you change the register function name to registerC2dm?

      — commented January 11th 2012 by Joonas Ruotsalainen
    • Thanks for your quick reply!

      When I changed in my tiapp.xml and used this code instead (https://gist.github.com/1173879) the register function finally worked. I got a response like 'JS registration success event: xxxxx'. This is the Auth token from google? (I'm really newbie to this)

      As I understand is my app ready to receive notifications now? Next step is to figure out how to send push notifications trhoug c2dm, wanna use Urban Airship. Any advice?

      Thanks!

      — commented January 11th 2012 by Åse Dittlau
    • The token you got from the registration success event is the registration id which you will need when sending push notification. I used php script from this thread to send push notifications.

      — commented January 11th 2012 by Joonas Ruotsalainen
    • Hi!

      I'm using push notifications on iphone with urbanairship, and now i want to do the same on android (also with urban)…

      So i'm also able to obtain my device ID on android, but i'm not sure on what to do now…i have tried to use the same function to register as in iphone but it doesn't work…

      Does anyone know how can i do it?

      Thx in advance!

      — commented January 23rd 2012 by Luis Cañamares
    • Hi I am using same procedure and getting response like Requested module not found: com.findlaw.titanium.c2dm

      — commented February 29th 2012 by veibhav d
    • If you are using the c2dm module from here: https://github.com/Kuraturpa/titanium-c2dm then you must replace all occurences and of course the modulepath to com.findlaw.c2dm instead of com.findlaw.titanium.c2dm. Especially the folder name, the tiapp.xml and the timodule.xml must be modified.

      Good luck.

      — commented February 29th 2012 by Robert Schmidt
  • Hi!!!

    I tried to use this module and stuck with this error:

    
    [ERROR] Error(s) compiling generated Java code
    [ERROR] path_to_project\sample\build\android\gen\com\_mycompany_\_projectName_\_projectName_Application.java:44: cannot find symbol
    symbol  : class C2dmBootstrap
    location: package com.findlaw.titanium.c2dm
            runtime.addExternalModule("com.findlaw.titanium.c2dm", com.findlaw.titanium.c2dm.C2dmBootstrap.class);
                                                                                            ^
    1 error
    
    — answered June 26th 2012 by Andrew Vesel4ak
    permalink
    4 Comments
    • Does anyone know how to fix?

      Thanks in advance for your answers!

      — commented June 26th 2012 by Andrew Vesel4ak
    • Why not use Ti.cloud instead of this module?

      — commented June 26th 2012 by Zane Wright
    • The bosses wants to urban)))

      — commented June 26th 2012 by Andrew Vesel4ak
    • Any news on the error above, C2dmBootstrap?
      I have got the same error using another one: http://developer.appcelerator.com/question/135058/c2dm-in-titanium-182#comment-131931

      — commented July 8th 2012 by Harry Hartman
  • Nothing yet.

    — answered September 8th 2010 by Don Thorp
    permalink
    1 Comment
    • @don, can you confirm if this in in the works, also if I will need to code for it differently than I do for the iPhone. It would be worth it for me to upgrade to premium if I knew that C2dm (or urban airship) was working for android for premium.

      — commented September 13th 2010 by Anthony Webb
  • Please can some one send me complete running project ?

    — answered October 17th 2012 by Ali Akram
    permalink
    0 Comments
  • Hi all, there's a new GCM module in the Marketplace called uPush for Android push messages, got it working with my project today in about 10 minutes, saved me so much time.

    — answered November 9th 2012 by Alan Browne
    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.