简体   繁体   中英

How can I create notification that is different on device and wear?

Basically, I am wondering if it is possible to create two different notifications and how - one for Android Device and other for Android Wear?

For example: I want to have just setContentText , but on Android device I want setContentTitle and setContentText

There is currently no possibility to show notification just on Wear (like setLocalOnly with device only - look for more ).

I think the Synchronized Notifications sample that comes with the Android Wear SDK may be useful to look at. It gives three simple types of notifications: (1) A phone-only notification (2) A watch-only notification (3) A pair of synchronized phone and watch notifications where the content shown on the watch notification is different from the one on the phone. They are synchronized in the sense that dismissing one results in dismissal of the other one; all based on the Data Layer apis.

I think the third use case is most relevant to you.

Officially it is not possible to create two different notifications for wear and the phone without writing your own Android Wear App extension. It is only possible to define a notification that is only shown on the phone with NotificationCompat.Builder.setLocalOnly(true)

To create a Notification that is only shown on a Wear Device however you can (at the moment) add the Notification to a group with NotificationCompat.Builder.setGroup(randomGroupKey) and omit the display of a group summary notification. If a notification belongs to a group it is not displayed on the phone because the phone will only show the summary notification. If there is no summary you get a notification for your watch only. Just generate a random group key for every watch only notification.

Officially it is only possible to create a notification that looks different on a smartwatch.

For this use a WearableExtender . For example this code snippet:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle(getString(R.string.smaple_notification_title));
builder.setSmallIcon(R.drawable.ic_message);
builder.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, ActivateActivity.class), PendingIntent.FLAG_UPDATE_CURRENT));

NotificationCompat.WearableExtender extender = new NotificationCompat.WearableExtender();
extender.setBackground(BitmapFactory.decodeResource(getResources(), R.drawable.notif_background));
extender.setContentIcon(R.drawable.ic_message);
extender.setHintHideIcon(true);
extender.extend(builder);

builder.setPriority(NotificationCompat.PRIORITY_LOW);
builder.setContentText(notificationText);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.large_icon));
notificationManager.notify(messageIndex, builder.build());

Sets a special background for the notification, hides the app icon that is normally displayed on the notification, and adds a new icon to the preview of your Notification in the "screen off" mode of the watch.

I don't know if there is a way to do exactly what you want but I try to use stack & summary to bypass this: an contentText only notification has been hidden by a summary notification with contentText and contentTitle. On the Android Wear however summary is not being displayed but all the stacked notification (in your term is the notification with only contentText) can be shown.

Yes, It is possible. Steps -

  1. Intercept your Notifications on Handheld by implementing BroadcastReceiever
  2. Generate Notification for Handheld using NotificationBuilder - use setLocalOnly to duplicating it with Wearable
  3. Send Notification data in Message to Wearable - using MessageApi
  4. Extract receieved data & generate Notification for Wearable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM