1

I am using push notifications in Android. When I receive a push notification and click on it it launchs an activity as i want , but if this activity it's already opened nothing happened , the activity keeps the old data.

AndroidManifest.xml

 <activity
        android:name=".Detail"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/title_activity_detail"
        android:launchMode="singleTop"
        android:parentActivityName=".Home"
        android:theme="@style/Theme.AppCompat.Light.DarkActionBar" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="ma.via.marocagenda.Home" />
    </activity>

GCMIntentService.java

public class GCMIntentService extends GCMBaseIntentService {
...
private static void generateNotification(Context context, String message) {
    //int icon = R.drawable.abc_ic_go;
    int icon = R.drawable.ic_launcher;
    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, Detail.class);
    // put extra for details
    notificationIntent.putExtra("id", VAL_TAG_ID);
    notificationIntent.putExtra("titre", VAL_TAG_TITRE);

    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notification.defaults |= Notification.DEFAULT_LIGHTS;

    notificationIntent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
    //PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);
}

what can be wrong ?

leonidas79
  • 437
  • 2
  • 7
  • 19

2 Answers2

1

You Can override onNewIntent() in your Activity to get the Latest Intent From the Notification.

@Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent.hasExtra("key")){
            //do your Stuff
        }
    }
0

You may use Local broadcasts from your service whenever a push notification arrives.

For how to use Local broadcasts read here https://stackoverflow.com/a/8875292/826657

Let your activity register for this broadcast action, and when a push notification is received, update the activity from onReceive().

Also, being more specific using ActivityManager you can check that if you activity is on top, if true send the broadcast also, else just show the notification.

Read here for how to find the current foreground activity. https://stackoverflow.com/a/4753333/826657

Community
  • 1
  • 1
Rachit Mishra
  • 6,101
  • 4
  • 30
  • 51
  • as you can see in my code, in the generateNotification method i want launch the **detail** activity only when click on notification not when i receive it – leonidas79 Aug 31 '14 at 12:21