Passing a variable through an activity notification

Here is the code I have:

GCMIntentService:

Intent notificationIntent = new Intent (this, MainActivity.class); notificationIntent.setAction (Intent.ACTION_MAIN); notificationIntent.addCategory (Intent.CATEGORY_LAUNCHER); notificationIntent.putExtra ("screen", activityToShow); notificationIntent.addFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent intent2 = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder nBuilder = 
            new NotificationCompat.Builder(this)
            .setSmallIcon(smallIconToDisplayInGCM)
            .setLargeIcon(largeIconToDisplayInGCM)
            .setContentTitle(title)
            .setContentText(text)
            .setSound(sound)
            .setTicker(ticker)
            .setContentIntent(intent2)
            .setAutoCancel(true);

Primary activity:

Oncreate ()

onNewIntent (getIntent ())

New method outside onCreate ()

@Override public void onNewIntent (intention intent) {Bundle extras = intent.getExtras (); String tabNumber = "";

    if(extras != null) {
        tabNumber = extras.getString("screen");
        Log.d("TEMP", "Tab Number: " + tabNumber);
        if(tabNumber.equals("") || (tabNumber == null)) {
            // Set the active tab to be the Home Tab  (index = 0)
            mTabHost.setCurrentTab(0);
        } else {
            mTabHost.setCurrentTab(getTabToShow(tabNumber));
        }
    } else {
        mTabHost.setCurrentTab(0);
    }
}

It doesn't even get to Log.d () when I click the Push Notification button.

+3
1

, :

Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setAction(Intent.ACTION_MAIN); 
    notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    notificationIntent.putExtra("screen", "debugScreen"); 
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    NotificationManager nMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    PendingIntent intent2 = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder nBuilder = 
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Some Title")
            .setContentText("Some text")
            .setContentIntent(intent2)
            .setAutoCancel(true);
    nMgr.notify(0, nBuilder.build());

onNewIntent:

@Override 
public void onNewIntent(Intent intent){ 
    Bundle extras = intent.getExtras(); 
    String tabNumber;

    if(extras != null) {
        tabNumber = extras.getString("screen");
        Log.d("TEMP", "Tab Number: " + tabNumber);

    } else {
        Log.d("TEMP", "Extras are NULL");

    }
}

, , .

, , , activityToShow. , Intent.putExtra(...) , , , Integer, .

activityToShow , , ( ), , .

, .

+2

All Articles