0

Iam trying to display a notification on a Android Wear watch only. I don't want the notification to be shown on the phone (It has the app open on their), and I don't want it to be in a Activity directly (However, one of the actions should open a Activity).

To accomplish what I want I created a WearableListenerService with a onDataChanged. Within this method I try to create the notification. This is the code I use: @Override public void onDataChanged(DataEventBuffer dataEvents) { Log.d("Listener", "dataChanged");

    Intent actionIntent = new Intent(getBaseContext(), WatchMainActivity.class);
    actionIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent actionPendingIntent =
            PendingIntent.getActivity(getBaseContext(), 0, actionIntent,
                    PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationManagerCompat manager = NotificationManagerCompat.from(getApplicationContext());
    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());

    Log.d("listener", "Created things");

    builder.setContentTitle("Wearable test")
            .setContentText("Testing 123")
            .setOngoing(true)
            .setSmallIcon(R.drawable.ic_launcher)
    ;

    // Create the action
    NotificationCompat.Action action =
            new NotificationCompat.Action.Builder(R.drawable.ic_full_sad,
                    getString(R.string.label), actionPendingIntent)
                    .build();


    NotificationCompat.WearableExtender extender = new NotificationCompat.WearableExtender();
    extender.addAction(action)
            .setContentAction(0)
            .setHintHideIcon(true)
    ;

    Log.d("listener", "action created, extending...");

    builder.extend(extender);

    Log.d("listener", "extended");


    manager.cancel(101);
    Log.d("listener", "canceled");

    manager.notify(101, builder.build());

    Log.d("listener", "notified");


    Log.d("listener", "created");
}

And when looking at the log it results in this crash:

D/listener( 1075): listener created
D/Listener( 1075): dataChanged
D/listener( 1075): Created things
D/listener( 1075): action created, extending...
D/listener( 1075): extended
D/listener( 1075): canceled
W/dalvikvm(  597): threadid=20: thread exiting with uncaught exception (group=0xada2fd70)
E/AndroidRuntime(  597): FATAL EXCEPTION: NotificationCollectorService
E/AndroidRuntime(  597): Process: com.google.android.wearable.app, PID: 597
E/AndroidRuntime(  597): java.lang.ClassCastException: android.os.Bundle cannot be cast to android.app.Notification$Action
E/AndroidRuntime(  597):    at android.support.v4.app.NotificationCompatApi20.getActionsFromParcelableArrayList(NotificationCompatApi20.java:145)
E/AndroidRuntime(  597):    at android.support.v4.app.NotificationCompat$NotificationCompatImplApi20.getActionsFromParcelableArrayList(NotificationCompat.java:532)
E/AndroidRuntime(  597):    at android.support.v4.app.NotificationCompat$WearableExtender.<init>(NotificationCompat.java:1876)
E/AndroidRuntime(  597):    at com.google.android.clockwork.stream.StreamManager.stripRemoteViewsFromNotification(StreamManager.java:481)
E/AndroidRuntime(  597):    at com.google.android.clockwork.stream.StreamManager.maybeStripRemoteViewsFromNotification(StreamManager.java:473)
E/AndroidRuntime(  597):    at com.google.android.clockwork.stream.StreamManager.setItem(StreamManager.java:347)
E/AndroidRuntime(  597):    at com.google.android.clockwork.stream.StreamManager.onNotificationPosted(StreamManager.java:253)
E/AndroidRuntime(  597):    at com.google.android.clockwork.stream.NotificationCollectorService$ServiceHandler.handleMessage(NotificationCollectorService.java:264)
E/AndroidRuntime(  597):    at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(  597):    at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(  597):    at android.os.HandlerThread.run(HandlerThread.java:61)
D/listener( 1075): notified
D/listener( 1075): created

So there is something within the Notification that causes this crash. When I removed the extend call in the builder, it works fine, however the action is of course not added. Due to this, I assume I do something wrong within my Intent/PendingIntent or within the WearableExtender, but I have no idea what I do wrong. When I take the examples from the wear documentation, it results in the same crash.

Someone has any idea on how to solve this crash?

2 Answers2

1

I've just tested the code you posted in my onDataChanged callback within DataLayerListenerService (that extends WearableListenerService). But... wverything works fine... I've only changed the string in action title to my R.string.app_name + Name of activity. Apart from that - there is no changes.

  1. Try replacing your actionPendingIntent with null in Action buider (to eliminate the Intent and PendingIntent) and replace your getString with just hardcoded string just for a test.
  2. Are you sure you have the latest Support-v4 lib? Check if you have anything not up to date with AndroidWear image, support lib etc.
  3. Try to use standard Notification API instead of NotificationCompat - there is no need to use NotificationCompat on Watch:

http://developer.android.com/training/wearables/apps/creating.html

For notifications that appear only on the wearable (meaning, they are issued by an app that runs on the wearable), you can just use the standard framework APIs (API Level 20) on the wearable and remove the support library dependency in the mobile module of your project.

http://developer.android.com/training/wearables/apps/layouts.html

Note: When creating custom notifications on the wearable, you can use the standard notification APIs (API Level 20) instead of the Support Library.

Please try these steps, especially switching to standard Notification instead of NotificationCompat - but your code copied as it is working in my app without any problem:\

Maciej Ciemięga
  • 10,125
  • 1
  • 41
  • 48
  • I switched to the standard API and that indeed seems to fix it. Not sure what caused the issue, but I guess this solves my issue for now. Thanks! – Paul Sohier Jul 12 '14 at 18:41
  • Glad I could help, but may I ask why my answer got unaccepted? I've tested by myself the entire code you posted and it's everything OK with it. As I said this issue is caused probably by your configuration or wrong libs versions (you said that "When I take the examples from the wear documentation, it results in the same crash."). I also gave you a clear solution to your question ("how to solve this crash?") - and you admit that it helped you and resolves the problem you had. And now it got unaccepted? I'm confused... is there anything wrong with my answer? – Maciej Ciemięga Jul 13 '14 at 16:22
0

Switching to the standard 4.4W API can fix it, when you use the support.v4 package, the Process com.google.android.wearable.app will crash. With support.v4 package, if you don't want to add RemoteInput in your notification, you can add action with build.addAction(action) instead of build.extend(extender.addAction(action))

Chaos
  • 120
  • 11