Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 4.1.1 Build Stack for Activity when receiving a Notification with URI

Our app has different notifications that can open different Activities. So we created the URI scheme to do so. The Notifications are received and open the correct activities. I create the stack for proper navigation with the following code:

Intent intent = new Intent(Intent.ACTION_DEFAULT, Uri.parse(uri));

TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntentWithParentStack(intent);

PendingIntent contentIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationManager mNotifM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder mBuilder = new Notification.Builder(context);
mNotifM.notify(NotificationId.getID(), mBuilder.setStyle(new    Notification.BigTextStyle(mBuilder)
            .bigText(bigText)
            .setBigContentTitle(title)
            .setSummaryText(summaryText))
            .setContentTitle(title)
            .setSmallIcon(R.drawable.udechile_launcher)
            .setContentText(summaryText)
            .setAutoCancel(true)
            .setContentIntent(contentIntent)
            .setTicker(bigText)
            .build());

The problem is that in Android 4.1.1 that code to recreate the stack does not work properly. The only way I made it work is referecing the class instead of the uri when creating the intent:

intent = new Intent(context, MatchDetail.class);

The problem with this is that I will have to do a Switch-Case for every uri to be able to create the intent with each class. This defeats the purpose of the URI in the first place. Also if in the future I need to add a new Push Target is not just adding the URI in the AndroidManifest.xml I have to add a new case in the switch of the Push Notification Receiver.

Does somebody knows how to make this work in Android 4.1.1 with URIs?

Extract of Manifest:

<activity
        android:name=".controller.MatchDetail"
        android:label="@string/title_activity_match_detail"
        android:parentActivityName=".controller.MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".controller.MainActivity" />
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:scheme="scheme" android:host="base" android:path="/name" />
        </intent-filter>
    </activity>
like image 654
Franklin Avatar asked May 01 '15 17:05

Franklin


1 Answers

I haven't tried this yet, but this could possibly fix your problem:

TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(Class.forName(intent.getComponent().getClassName()));
stackBuilder.addNextIntent(intent);

If that doesn't work, try this:

TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(context, Class.forName(intent.getComponent().getClassName()));
stackBuilder.addNextIntent(intent);

Also one more to try:

TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntentWithParentStack(new Intent(context, Class.forName(intent.getComponent().getClassName())));

Edit

Found a new way that might work, hopefully

TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(new ComponentName(context, Class.forName(intent.getComponent().getClassName())));
stackBuilder.addNextIntent(intent);
like image 129
Kevin van Mierlo Avatar answered Oct 07 '22 00:10

Kevin van Mierlo