Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Variable through Notification to Activity

Here is the code that 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);

Main Activity:

onCreate()

onNewIntent(getIntent()) 

New Method outside of onCreate()

@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);
        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's not even hitting the Log.d() when I click on the Push Notification.

like image 445
Sohel Mansuri Avatar asked Dec 20 '22 18:12

Sohel Mansuri


1 Answers

I've implemented your code with slight modifications to make some debugging:

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());

And the onNewIntent method:

@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");

    }
}

I could check it works fine and the variable gets passed to the activity in the intent.

What I think you might be doing wrong is setting the intent data with the variable activityToShow. Take into account that Intent.putExtra(...) is an overloaded method, and if you pass, for instance, an Integer, you won't be able to get a String.

I recommend you to monitor the state of the activityToShow variable, and add some logging to the receiver, to check what you get (including the variable type) and what you receive in the activity.

Hope it helps.

like image 131
Junior Buckeridge Avatar answered Jan 02 '23 18:01

Junior Buckeridge