Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6583233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:22:56+00:00 2026-05-25T16:22:56+00:00

I am trying to display messages from C2DM so When I start an Activity

  • 0

I am trying to display messages from C2DM so When I start an Activity from a non activity it works but only first time. My activity contains View and close button so if the user is not inside the app when he receive the push notification then it should start the app when he press view button or close it on close this works as well but when user press view button and enters the app after than I am unable to start the activity again, it seems like the activity is there but its not visible to user. My code is as below , hope you understand what I am saying. The C2DM code example used is taken from here

C2DMReceiver

package com.mks.android.example;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.google.android.c2dm.C2DMBaseReceiver;
import com.google.android.c2dm.C2DMessaging;
import com.mks.android.example.activity.MessageDialogActivity;

public class C2DMReceiver extends C2DMBaseReceiver {
    private static String senderId = "mysenderemail@gmail.com";
    String registrationId;
    public C2DMReceiver() {
        super(senderId);
    }

    @Override
    public void onRegistrered(Context context, String registrationId) {
        this.registrationId = registrationId;
        Log.e("Error", registrationId);
    }

    @Override
    public void onUnregistered(Context context) {
        C2DMessaging.register(context, senderId);
    }

    @Override
    public void onError(Context context, String errorId) {
        Log.w("Error", errorId);
    }

    @Override
    protected void onMessage(Context context, Intent intent) {
        Log.i("Error", "MessageReceived");
        Bundle bundle = intent.getExtras();
        intent = new Intent();
        intent.putExtra("messageBundle", bundle);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);       
        intent.setClass(context, MessageDialogActivity.class);
        startActivity(intent);      
        // load message display activity here       
    }
}

MESSAGE DIALOG ACTIVITY CLASS

package com.mks.android.example.activity;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.mks.android.example.R;
import com.mks.android.example.activity.list.Functions;

public class MessageDialogActivity extends Activity {
    private Functions func = new Functions();
    private int isAppActive = 0;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showMessages(); 
    }
    private void showMessages(){
        Bundle bundle = getIntent().getBundleExtra("messageBundle");
        String title = bundle.getString("title");
        String message = bundle.getString("message");
        setContentView(R.layout.c2dm_message);

        TextView txtTitle = (TextView) findViewById(R.id.title);
        TextView txtMessage  = (TextView) findViewById(R.id.message);
        Button btnView = (Button) findViewById(R.id.btnView);
        Button btnClose = (Button) findViewById(R.id.btnClose);
        txtTitle.setText(title);
        txtMessage.setText(message);

        this.isAppActive = func.getAppState(this);

        if(isAppActive==1){
            btnView.setVisibility(View.GONE);
        }       

        btnView.setOnClickListener(new OnClickListener() {          
            @Override
            public void onClick(View v) {
                if(isAppActive == 0){
                    Intent intent = new Intent(Intent.ACTION_MAIN);
                    intent.setComponent(new ComponentName("com.mks.android.example","com.mks.android.example.activity.MainActivity"));
                    startActivity(intent);                  
                }
                MessageDialogActivity.this.finish();
            }
        });

        btnClose.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();               
            }
        });
    }
     @Override
        protected void onNewIntent(Intent intent) {
            setIntent(intent);
            showMessages();
        }
}

AndroidManifest FIle

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.mks.android.example"
      android:versionCode="1"
      android:versionName="1.0" android:installLocation="preferExternal">
    <uses-sdk android:minSdkVersion="4" />
    <application android:label="@string/app_name" android:icon="@drawable/icon_launcher" android:debuggable="true">
        <uses-library android:name="com.google.android.maps" />
        <activity android:name=".activity.MainActivity" 
                  android:label="@string/app_name"
                  android:screenOrientation="portrait"
                  android:theme="@android:style/Theme.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".activity.MessageDialogActivity" android:theme="@style/DialogNoTitle" android:activity android:launchMode="singleTop" />

      <service android:name=".C2DMReceiver" />           
      <!-- Only C2DM servers can send messages for the app. If permission is not set - any other app can generate it --> 
      <receiver android:name="com.google.android.c2dm.C2DMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND">
          <!-- Receive the actual message -->
          <intent-filter>
              <action android:name="com.google.android.c2dm.intent.RECEIVE" />
              <category android:name="com.mks.android.example" />
          </intent-filter>
          <!-- Receive the registration id -->
          <intent-filter>
              <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
              <category android:name="com.mks.android.example" />
          </intent-filter>
      </receiver>
    </application>
    <supports-screens android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:anyDensity="true" />
<permission android:name="com.mks.android.example.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.mks.android.example.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive message -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest> 

If i remove androidLaunchMode singleTop form manifest file and add set flag to multipleTask in C2DMReciver class it works but i get lots of dialog for each message. Thanks for help .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:22:56+00:00Added an answer on May 25, 2026 at 4:22 pm

    Set flag to singletop , the code to call messagedialogactivity is as under:

    Intent intent = new Intent();
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);      
    intent.setClass(context, MessageDialogActivity.class);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to set up a messages view with asp.net mvc that will display
I am trying to display some message in span. but it is not working.
Trying to display current time with PHP (using this ): $date = date('m/d/Y h:i:s
When trying to display a byte stream from HLDS (Half-Life Dedicated Server) in a
I've been trying to display text using a Quartz context, but no matter what
I'm trying to display a series of titles varying from 60 characters to 160
I'm trying to pull some data from plist file and display it in a
I'm trying to display a small text (new messages count) over icon in toolbar
I am trying to write a simple utility webcontrol to display one-line messages inside
I'm trying to display a custom progressdialog while loading RSS feed from an HTTP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.