C2dm in titanium
Hi!!! Anyone knows about new c2dm for Android in Titanium? I see Push for iPhone but nothing about c2dm.
Thanks¡¡¡
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
andmanifest
to a folder namedmodules/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 ofcom.findlaw.titanium.c2dm
with your project id (for instancecom.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.
-
Any news on this?
Thanks -
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?
-
Hello, I made the module work with 1.8.0.1. Github: titanium-c2dm
-
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.
-
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.
-
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?
-
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.
-
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!
-
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
-
Nothing yet.
-
Please can some one send me complete running project ?
-
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.