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

守給你的承諾、 提交于 2019-11-30 23:01:52

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!