0

I am using a service to receive some GCM messages from the could. I put the message into an extra and passing it to the class I want to start, there I display the message inside a dialog.

But the next time I get a message it should update the content of the intent opened earlier, the problem is that I get an exception and my app crashes, eclipse is forcing me to use "FLAG_ACTIVITY_NEW_TASK" to solve the crash, but this way my "PendingIntent.FLAG_UPDATE_CURRENT" will be ignored.

How could I update the previously opened intent? Creating a third activity and passing the data back and fort seems to me a bad solution.

Service class:

      ...

private void sendNotification(String msg, Bundle extras)
        {
        mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent intent = new Intent(this, DriverDetails.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        // If this is a notification type message include the data from the message 
        // with the intent
        if (extras != null)
        {
            intent.putExtras(extras);
            intent.putExtra("message", msg);
            intent.setAction("com.myGcm.NOTIFICATION");
            startActivity(intent);
        }
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder = 
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_cloud)
            .setContentTitle("GCM Notification")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg)
            .setTicker(msg)
            .setAutoCancel(true)
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
        }

...
Edmond Tamas
  • 3,148
  • 9
  • 44
  • 89

1 Answers1

1

You're solution should be LocalBroadcastManager. It is pretty simple: just register a listener in the onResumeof your Activity and fire an Intent with the Update-Information from anywhere else. I attached two links wich should guide you ;)

how to use LocalBroadcastManager?

http://www.intertech.com/Blog/using-localbroadcastmanager-in-service-to-activity-communications/

Community
  • 1
  • 1
Stefan Medack
  • 2,731
  • 26
  • 32