vendredi 8 mai 2015

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>

Aucun commentaire:

Enregistrer un commentaire