androidwear-osandroid-wear-notification

Launching an activity on micro app (android wearable)


I am creating notification from my app on Phone in the below way.

   private void launchMicroAppFromWearableNotif() {

   int notificationId = 001;

    // The below action has been defined in the micro app
    Intent i = new Intent("com.microapp.action.PLAY_MUSIC");
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent playPendingIntent =
            PendingIntent.getActivity(this, 0, i, 0);

    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.play_music)
                    .setContentTitle("Play Music")
                    .setContentText("Play Music on Wear")
                    .setContentIntent(playPendingIntent)
                    .addAction(R.drawable.play_music, getString(R.string.act1), playPendingIntent);

    NotificationManagerCompat notificationManager =
            NotificationManagerCompat.from(this);

    // Build the notification and issues it with notification manager.
    notificationManager.notify(notificationId, notificationBuilder.build());
}

I have created a wearable micro app which has an activity(PlayActivity) with action "com.microapp.action.PLAY_MUSIC" defined in wearable Manifest.

When I take the action from notification on the wearable I expect the activity form the micro app to be launched. But nothing happens. Could someone please help on this. Is it the right way to do it?


Solution

  • Actions that you add to a notification are for the device that the notification was created on, so if you create a notification on your phone, when you click on it on your watch, the action will be sent back to your phone to be executed.

    If you want to open your activity on your watch, you cannot achieve that by sending a notification from your phone in the manner you have done. You have a couple of options: for example you can send a message from your phone to your watch and then have the wear side of your app catch that message and put out a "local" notification on the watch; then the actions on that notification are local to your watch and you can open your desired activity via the actions in that notification. If you directly want to open an activity on your watch from your phone (i.e. you don't need a notification to show up on your watch), then you can handle the message that I talked about on your watch and instead of creating a notification there, simply open the desired activity.You may find the WearCompanionLibrary useful in handling some of these cases.