Gcm or C2DM application launch problem.

You must Login before you can answer or comment on any questions.

Hi, I am developing small android application in which I want to integrate GCM. I used one module for it and its working fine. The only problem with it is when my application is open and if I click on notification it relaunch my application which I don't want.. What I want if application is already running then just show running window and if application is closed then launch application...

In my module code for onmessage received looks like

int icon = 0x7f020000;
 
                CharSequence tickerText = new String("Votodo: " + hashdata.get("messages"));
                long when = System.currentTimeMillis();
 
                CharSequence contentTitle = "Votodo";
                CharSequence contentText = new String(" " + hashdata.get("messages"));
 
                Intent notificationIntent = new Intent(this, GCMIntentService.class);
 
                Intent launcherintent = new Intent("android.intent.action.MAIN");
                launcherintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
 
                launcherintent.setComponent(ComponentName.unflattenFromString("com.example/com.example.ExampleActivity"));
                launcherintent.addCategory("android.intent.category.LAUNCHER");
 
                PendingIntent contentIntent = PendingIntent.getActivity(this, 0, launcherintent, 0);
 
                Notification notification = new Notification(icon, tickerText, when);
 
                notification.defaults = Notification.DEFAULT_ALL;
                notification.flags = Notification.FLAG_AUTO_CANCEL;
                notification.setLatestEventInfo(context, contentTitle, contentText,contentIntent);
                String ns = Context.NOTIFICATION_SERVICE;
                NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
                mNotificationManager.notify(1, notification);
My module is working fine if application is closed.. But relaunch application which is already running which is not expected...... Need Help..... Thank you...........

2 Answers

Hi!, i found a solution for this, but it still has a little problem wich i havent worked to reolsve yet since i was actually looking for tips to use GCM, well, in order to bring the titanium app to foreground when its already running im creating the intent as follows:

var intent = Ti.Android.createIntent({
    flags: Ti.Android.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Titanium.Android.FLAG_ACTIVITY_CLEAR_TOP | Titanium.Android.FLAG_ACTIVITY_SINGLE_TOP,
    className : 'org.appcelerator.titanium.TiActivity',
    packageName: 'myApp.Package'
    });
So i guess you should try using "org.appcelerator.titanium.TiActivity" instead of "com.example/com.example.ExampleActivity", and also you should look for the corresponding property to set your packageName "com.example/com.example.ExampleActivity", the problem with this is that if the app was closed due to user inactivity then the app comes to front totally blank, and stays stuck like that until the user closes it by pressing the back button.

I think there must be a better way to do this, but i havent found it yet if it exist.

Btw, can you tell me what module are you using to integrate GCM?

Sorry about the bad english anyway

— answered 9 months ago by Rodrigo Gonzalez
answer permalink
4 Comments
  • Rodrigo are you using any module for GCm ?? I using this module..... So My callback function is not working when my application is closed mean my application not able to generate notification when It is closed. Becoz of that reason I am generating notification in side module method: onmessage received . In this module I am generating notification..... SO not able to use org.appcelerator.titanium.TiActivity any other solution need help....

    — commented 9 months ago by nilesh kashid

  • Hi again,

    As i see in android documentation, looks like theres some intent“s methods one should try in order to reference the titanium activity, so you could probably replace the related code line with this:

    //launcherintent.setComponent(ComponentName.unflattenFromString("com.example/com.example.ExampleActivity"));
    launcherintent.setClassName(ComponentName.unflattenFromString("com.example/com.example.ExampleActivity"), ComponentName.unflattenFromString("org.appcelerator.titanium.TiActivity"))
    I havent used it because im not even getting to connect with the module, im using the same module that you are using but im not even getting any response to the functions passed into gcm.registerC2dm(...), did you manage to make it work with only the example from the module“s developer?, can you possibly share a simple code wich works for you so i can adapt it for my proyect?, it would be a great help for me, btw i havent found any other module to connect with GCM

    — commented 9 months ago by Rodrigo Gonzalez

  • @Rodrigo thank you for reply. I am using same module with some changes and it's working fine. I want to clarify some points. If we use callback inside application file i.e. application.js file it's work fine if application is open but it's not when application is not open because it is unable to call that callback function. similar kind of issue was occur in C2DM also.

    I am generating notification inside module code where my broadcast receiver receiving message from server. I tried your code in ans. its working fine i.e. it helps me to avoid relaunch of application if it is already in running status but again I am not able to open my application through notification if it closed.

    So now what I am doing is set one flag through set method inside kroll.module method when my application is open => set to true if close then => set false according to that generate notification with two diff set of flags and its working fine.....

    If you need my help then definitely I will help I will give you my module code so that you can also integrate it with your project

    thank you....

    — commented 9 months ago by nilesh kashid

  • Show 1 more comment

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.

Your Answer

Think you can help? Login to answer this question!