Take the 2-minute tour ×

I have an app with a textbox that displays the content of a notification that I send out through a test server. The app works fine, when it is in the foreground or background (when I press the home key).

The GCM demo code that I'm using brings the app back into focus and fills in the textbox correctly, once I tap the notification.

When I press the back key or I exit the app from the menu, I can still receive notifications. But, when I click on the notification, the app opens and the textbox remains blank.

I know that stopping the app causes the onDestroy() method to essentially kill everything. All of my handlers, etc are now gone. So how is it possible to view notification content after the app is stopped?

From GCMIntentService.java (this is the boilerplate code, nothing changed here):

 * 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.ic_stat_gcm;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager)
    Notification notification = new Notification(icon, message, when);
    String title = context.getString(R.string.app_name);
    Intent notificationIntent = new Intent(context, DemoActivity.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_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);

My handler to set the textbox (this is in the MainActivity):

    private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
        mDisplay.append(newMessage + "\n");
        String regid = intent.getExtras().getString("regid");

My Manifest if fine since I'm able to register, unregister and receive messages.

share|improve this question
It's impossible to answer your question without seeing your code. Please add the code you use to initialize/update that textbox. –  Eran May 16 '13 at 18:24
OK, I added the code. Let me know if you need to see more. –  user2391115 May 16 '13 at 19:02
GCMIntentService is not a class you are supposed to use as is (it's just a demo). You are supposed to override GCMBaseIntentService yourself. The generateNotification method opens DemoActivity when the notification is tapped. It doesn't open your main activity (unless DemoActivity is your main activity). And onMessage, which calls generateNotification doesn't pass to it the content of the notification (it passes a string resource). So it's not clear what notification content you are trying to display and how you intend it to be displayed. –  Eran May 16 '13 at 19:45

Your Answer



Browse other questions tagged or ask your own question.