vendredi 8 mai 2015

Why can't I start an Activity from a page Notification on Android Wear?

I just succesfully created a bunch of pages notifications on my Wear device.

The only problem is that the PendingIntent does not seems to start an Activity (which is of course declared in Manifest).

Here is my code:

List extras = new ArrayList();
Intent viewIntent = new Intent(getApplicationContext(), DetailActivity.class);
viewIntent.putExtra("KEY", "TEST123");
//Note: I also tried: Intent viewIntent = new Intent(getApplicationContext(), DetailActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent viewPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, viewIntent, 0);

for (Route aRoute : myRoutes) {
    Notification aNotif = new NotificationCompat.Builder(getApplicationContext())
    .setContentTitle("BUS " + aRoute.route_short_name)
    .setContentText(aRoute.directions.get(0).trip_headsign)
    .setLargeIcon(bitmap)
    .setContentIntent(viewPendingIntent)
    .setSmallIcon(R.mipmap.ic_launcher).build();

    extras.add(aNotif);
}

NotificationCompat.Builder builder1 = new NotificationCompat.Builder(this)
    .setContentTitle(title)
    .setContentText(desc)
    .setContentIntent(viewPendingIntent)//Just in case
    .setSmallIcon(R.mipmap.ic_launcher);

Notification notification = builder1
    .extend(new NotificationCompat.WearableExtender()
    .addPages(extras))
    .setContentIntent(viewPendingIntent)//Just in case
    .build();

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    notificationManager.notify(0, notification);

When I press on a Notification, I expect the intent to start, but nothing happens..

Any suggestion is welcome.

EDIT: This code works, just after the notification, so, the second activity can easily be launched withour bug:

startActivity(new Intent(getApplicationContext(), DetailActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));

Aucun commentaire:

Enregistrer un commentaire