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

  • Home
  • SEARCH
  • 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 8632215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:19:17+00:00 2026-06-12T09:19:17+00:00

I have two applications installed on my emulator. The manager app sends a message

  • 0

I have two applications installed on my emulator. The “manager” app sends a message to GCM. The other app called “scheduler” receives the message and displays a notification to the user.

When I have both applications running at the same time on the emulator, the manager app works fine. However, the scheduler app crashes after GCMIntentService processes onMessage(); This only happens on the first message. All remaining messages are processed without the application crashing.

No errors are printed out in the logcat.

Questions

  1. Is there exception handling for GCMIntentService?
  2. If the app is not visible and GCMIntentService calls a function to open an Activity. Can this crash the program?
  3. If I have two apps running on the emulator at the same time could it be a problem with the emulator?

Here is the GCMIntentSerive code:

/**
 * {@link IntentService} responsible for handling GCM messages.
 */
public class GCMIntentService extends GCMBaseIntentService {
    @SuppressWarnings("hiding")
    private static final String TAG = "GCMIntentService";
    // Request code
    public static final int CUSTOM_REQUEST_CODE_ENTER_TEXT = 666;

    public GCMIntentService() {
        super(SENDER_ID);
    }

    @Override
    protected void onRegistered(Context context, String registrationId) {
        Log.i(TAG, "Device registered: " + "regId = " + registrationId);
      //  displayMessage(context, getString(R.string.gcm_registered));
        ServerUtilities.register(context, registrationId);

        displayError(context, "Device registered for Notifications from Anime Convention");
        System.out.println("Device registered: " + "regId = " + registrationId);
    }

    @Override
    protected void onUnregistered(Context context, String registrationId) {
        Log.i(TAG, "Device unregistered");
       // displayMessage(context, getString(R.string.gcm_unregistered));
        if (GCMRegistrar.isRegisteredOnServer(context)) {
            ServerUtilities.unregister(context, registrationId);
            System.out.println("Device Unregistered: " + "regId = " + registrationId);
        } else {
            // This callback results from the call to unregister made on
            // ServerUtilities when the registration to the server failed.
            Log.i(TAG, "Ignoring unregister callback");
        }
    }

    @Override
    protected void onMessage(Context context, Intent intent) {
        Log.i(TAG, "Received message");
        //String message = getString(R.string.gcm_message);

        System.out.println("onMessage: ");


        Bundle extras = intent.getExtras(); 

               String message =extras.getString("message");
               String event_id_from_server =extras.getString("server_id");
           //    displayMessage(context, message);
                generateNotification(context, message);
                saveMsg(message);    

                System.out.println("server id is "+ event_id_from_server);

                updateLocalDatabase(event_id_from_server);
        }


    @Override
    protected void onDeletedMessages(Context context, int total) {
        Log.i(TAG, "Received deleted messages notification");
        String message = getString(R.string.gcm_deleted, total);
       // displayMessage(context, message);
        // notifies user
        generateNotification(context, message);
    }

    @Override
    public void onError(Context context, String errorId) {
        Log.i(TAG, "Received error: " + errorId);

        if(errorId.equals("ACCOUNT_MISSING")){


        String error="Anime Convention Scheduler was unable to register your device for notifications. You need to add a GMAIL account to the phone inorder to use this service. Then use the Options Menu to register";

        displayError(context, error);
        displayMessage(context, error);
        }


     // save using saved preferences than display.
        if(errorId.equals("SERVICE_NOT_AVAILABLE")){

            String error="Google Cloud Messageing Service is not currently available";

            displayError(context, error);
        }


        // save using saved preferences than display.
        if(errorId.equals("AUTHENTICATION_FAILED")){
            String error="Google Cloud Messageing did not recognized your password. Please re-enter your password for your GMAIL account";
            displayError(context, error);
        }

        // save using saved preferences than display.
        if(errorId.equals("PHONE_REGISTRATION_ERROR") || errorId.equals("INVALID_PARAMETERS")){

            String error="Your phone does not support Google Cloud Messageing. You will not receive notifications from Anime Convention";
            displayError(context, error);
        }
    }

    @Override
    protected boolean onRecoverableError(Context context, String errorId) {
        // log message
        Log.i(TAG, "Received recoverable error: " + errorId);
     //   displayMessage(context, getString(R.string.gcm_recoverable_error,               errorId));

        return super.onRecoverableError(context, errorId);
    }

    /**
     * Issues a notification to inform the user that server has sent a message.
     */
    private static void generateNotification(Context context, String message) {
        int icon = R.drawable.icon;
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);
        String title = context.getString(R.string.app_name);
        Intent notificationIntent = new Intent(context, TabBarExample.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, notification);
    }

    private String handleMessage(Intent intent) {
        String id = intent.toString();
        String message = null;
        try {
            JSONObject json = new JSONObject(id);
             message = json.getString("message_id");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return message;
    }

    public void saveMsg(String msg) {
        boolean worked = true;
        try {
            NotificationsDatabase entry = new NotificationsDatabase(GCMIntentService.this);
            entry.open();
            java.util.Date date= new java.util.Date();
             Timestamp x = new Timestamp(date.getTime());

            String timeStamp=x.toLocaleString();
            entry.createEntry(msg,timeStamp);

            entry.close();

            NoteAdapter note = null;
            note.notifyDataSetChanged();

            NewsRowAdapter nra  = null;
            nra.notifyDataSetChanged();

            AlertAdapter aa=null;
            aa.notifyDataSetChanged();
        } catch (Exception e) {
            worked = false;
            String error = e.toString();
            System.out.println(error);
        } finally {
            if (worked) {
            }
        }
    }
    @Override
    public void onDestroy() {
        GCMRegistrar.onDestroy(GCMIntentService.this.getApplicationContext());
        super.onDestroy();
    }

    public void updateLocalDatabase(String serverId){

        List<Alerts> listAlerts;

        int server_id=Integer.parseInt(serverId);

        DatabaseSqlite entry = new DatabaseSqlite (GCMIntentService.this);
        entry.open();
        listAlerts = entry.getData();       
        entry.close();

        int alerts=listAlerts.size();


        for (int i = 0; i < alerts; i++) {
            Alerts item = listAlerts.get(i);

        int remote_id =item.getRemoteServerId();
        String eventName=item.getEventName();
        int local_id=item.getRowId();

        if(server_id ==remote_id){

            //update database with new info

            // Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag
            Intent intent = new Intent(GCMIntentService.this, UpdateLocalDatabase.class);
            intent.putExtra("server_id", server_id);
            intent.putExtra("local_id", local_id);
            intent.putExtra("event_name", eventName);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            startActivity(intent);
            }
        }                   
    }
}
  • 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-06-12T09:19:18+00:00Added an answer on June 12, 2026 at 9:19 am

    There were two problems with the code that created this issue.

    1. I did not have a condition that checked if event_id_from_server was null or empty in the application.

    2. When you send Message to GCM, they must not be null or “”. That crashes the service.
      So I needed to create this condition in the php for messages that did not have an id associated with them:

      if(!$id || $id==””){

          $fields = 
      array(
      'registration_ids' => $registrationIDs, 
      'data' => array("message" => $message), 
      'delay_while_idle'=> false,
      'collapse_key'=>'core_update'
      );
      
      
      
      }else{
      
      $fields = 
      array(
      'registration_ids' => $registrationIDs, 
      'data' => array("message" => $message,"server_id"=>$id), 
      'delay_while_idle'=> false,
      'collapse_key'=>'core_update'
      );
      
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have two https web applications app1 and app2 installed on two different tomcats
I have two separated applications, one written in Java and other in Erlang. Both
I have two .NET applications that talk to each other over a named pipe.
I have two applications installed on my device,each with a service component in it
I have two applications setup in IIS7.5. MVC 3 is installed. One application serves
I have two applications Web Socket and Web Service created in .Net/Web Form and
I have two applications: c++ service and a RoR web server (they are both
I have two applications in my project 'test' the applications are one.mxml and two.mxml
public enum ObjectType { Country=0, Region=1, Province=2, City=3, Hotel=4 } I have two applications
I have two Android applications with a similar functionality but different drawables and layouts.

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.