Android Wear : Custom Notifications

戏子无情 提交于 2019-12-13 17:28:24

问题


In handheld devices, custom notifications can be displayed using RemoteViews. RemoteViews allows the developer to fully customise the notification.

What is the way to do the same for Android Wear? Which class should be used to override the default notification UI with my own customised one?


回答1:


  1. if you want to customize text only, you can use SpannableString. It allows you to change color, background, align of your title/content text.

  2. if you want to create totally different notification you have to implement smth similar in your wear project

    Intent notificationIntent = new Intent(context, WearNotificationActivity.class);
    PendingIntent pendingNotificationIntent =
            PendingIntent.getActivity(context, 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
    
        Notification notification =
                new Notification.Builder(context)
                        .setSmallIcon(R.drawable.ic_launcher)
    
                       // .setContentTitle("CustomNotification")
                        .extend(new Notification.WearableExtender()
                                .setDisplayIntent(pendingNotificationIntent)
                                .setCustomSizePreset(Notification.WearableExtender.SIZE_LARGE)
                                .setStartScrollBottom(false)
                                .setHintHideIcon(true))
    
                        .build();
    
        NotificationManager notificationManager =
                (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
    
        notificationManager.notify(0, notification);
    

where WearNotificationActivity - activity container of you custom view.

NOTE: you HAVE TO use .setSmallIcon(..) even if you don't need it. Seems like a google bug, but without this line notification won't be shown.

and set

 android:allowEmbedded="true"
 android:taskAffinity=""

for your activity container




回答2:


To create a rich Notification for Android Wear, you have to use NotificationCompat.Builder from support-v4.

This version gives you a better control of the notification layout on Wear with methods such as .setActionButton() or .setStyle().

You can even more customize your notification with a NotificationCompat.WearableExtender.

Learn more at Creating a Notification




回答3:


Currently there is no way to do that. EDIT: it is possible: Custom UI for Android Wear Notifications

Best option out there are:

  • Create Notification on phone and use NotificationCompat.WearableExtendernotification for both phone and Wear.
  • Create Notification with setLocalOnly() so notification will be limited to phone, and on Wear create separate one - with other look, actions etc.
  • Do as above but instead of notification on Wear create own app with CardFrames (that allows you to have "notification style" and custom layout at same time, and when receiving signal from phone run your app instead of notification.

So only last option allows you to have custom layout but there are many drawback (as it is own app) - for example it is separate from notification list.

Hope that might change in the future.



来源:https://stackoverflow.com/questions/27014866/android-wear-custom-notifications

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