Why can't I start an Activity from a page Noti

2019-09-01 17:28发布

问题:

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

EDIT2: There is now an "open" button at the end that works fine, but still nothing happens on individual notifications (every pages)

回答1:

  1. Pages are not clickable - on Android Wear, only actions are clickable. For phone generated notifications, those only appear after all pages
  2. If you have a content intent on your phone generated notification, that will always appear as an 'Open on phone' action. There is no way to disable this unless you remove your content intent (making the notification unclickable on phones).

I say 'phone generated' as you can also create a Wear app. By using the data layer to push messages to your Wear app, the Wear app can then build custom notifications. These notifications allow you to use setDisplayIntent() and display activities inline (either as the main page or as separate pages). These activities can, of course, contain any View you want, including actions to perform any action (such as send a message back to the phone to start a particular activity).

Note that because pages are not clickable by default, styling of a custom notification should make it very obvious that the items are clickable. Rather than using a custom notification activity, you may consider using setContentAction() to display the action icon inline with the rest of the layout - this removes the action as a separate element past the action and places it directly on the notification/page.