为android wear添加通知

为android wear创建通知

NotificationCompat.Builder创建一个可以在手机上发送到可android wear通知.当用这个类创建通知时,系统负责在合适的时候在手机或者wear上展示这个通知

注意:对于使用RemoteViews来创建布局的通知在wear上仅仅显示文本和图标.但是你可以使用自定义的卡片布局创建自定的通知应用运行在wear上.

导入必须的类

build.gradle文件中添加下面这个依赖添加必须的引用包

compile "com.android.support:support-v4:20.0.+"

在导入必要的依赖库之后就有权限访问一些包,可以导入以下类

import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.NotificationCompat.WearableExtender;

使用Notification Builder创建通知

上面导入的v4库允许你创建一个有新特性(操作按钮,大图标)的通知,同时兼容android1.6及更高版本.

创建一个NotificationCompat.Builder实例,使用notify()方法来展示一个通知.

int notificationId = 001;
// Build intent for notification content
Intent viewIntent = new Intent(this, ViewEventActivity.class);
viewIntent.putExtra(EXTRA_EVENT_ID, eventId);
PendingIntent viewPendingIntent =
        PendingIntent.getActivity(this, 0, viewIntent, 0);
NotificationCompat.Builder notificationBuilder =
        new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_event)
        .setContentTitle(eventTitle)
        .setContentText(eventLocation)
        .setContentIntent(viewPendingIntent);
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =
        NotificationManagerCompat.from(this);
// Build the notification and issues it with notification manager.
notificationManager.notify(notificationId, notificationBuilder.build());

当通知到达手机时,用户可以通过点击通知获取通过setContentIntent()该方法设置的PendingIntent.当在wear上时通过向左滑动通知,来打开这个PendingIntent.

添加操作按钮

除了通过setContentIntent()设置主要内容外,还可以通过addAction()PendingIntent添加其他行为.

下面的代码展示一个相同的通知,但是添加了一个操作在地图上查看事件.

// Build an intent for an action to view a map
Intent mapIntent = new Intent(Intent.ACTION_VIEW);
Uri geoUri = Uri.parse("geo:0,0?q=" + Uri.encode(location));
mapIntent.setData(geoUri);
PendingIntent mapPendingIntent =
        PendingIntent.getActivity(this, 0, mapIntent, 0);
NotificationCompat.Builder notificationBuilder =
        new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_event)
        .setContentTitle(eventTitle)
        .setContentText(eventLocation)
        .setContentIntent(viewPendingIntent)
        .addAction(R.drawable.ic_map,
                getString(R.string.map), mapPendingIntent);

在手机上,这个操作像是一个附加在通知上的附加按钮.在wear上当用户向左滑动通知时这个操作像是一个大按钮.当用户触碰这个操作,此时这个关联的PendingIntent会显示.

提示:如果你的通知包含一个回复操作(比如消息应用),你可以通过wear的语音输入回复来增强行为.更多信息操作这里

wear上不一样的操作

如果你想wear上显示的行为操作和手机上的不一样,那么可以使用WearableExtender.addaction()这个方法.一旦使用了这个方法,在wear上不会在显示通过NotificationCompat.Builder.addAction()添加的行为操作.也就是说只有通过WearableExtender.addaction()添加的行为操作会显示在wear上不会显示在手机上.

// Create an intent for the reply action
Intent actionIntent = new Intent(this, ActionActivity.class);
PendingIntent actionPendingIntent =
        PendingIntent.getActivity(this, 0, actionIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

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

// Build the notification and add the action via WearableExtender
Notification notification =
        new NotificationCompat.Builder(mContext)
                .setSmallIcon(R.drawable.ic_message)
                .setContentTitle(getString(R.string.title))
                .setContentText(getString(R.string.content))
                .extend(new WearableExtender().addAction(action))
                .build();

添加一个大视图

通过向通知中添加一个大视图样式,可以在通知中插入扩展的文字内容.在手机上用户可以通过扩展的通知看到这个内容.在wear上这个大视图内容默认时显示的.

通过NotificationCompat.Builder的对象调用setStyle()方法添加扩展的内容.该方法可以传递BigTextStyle或者InboxStyle的对象实例

举个例子,下面的代码向通知中添加一个 NotificationCompat.BigTextStyle实例对象,以便包括完整的事件描述

// Specify the 'big view' content to display the long
// event description that may not fit the normal content text.
BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
bigStyle.bigText(eventDescription);

NotificationCompat.Builder notificationBuilder =
        new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_event)
        .setLargeIcon(BitmapFactory.decodeResource(
                getResources(), R.drawable.notif_background))
        .setContentTitle(eventTitle)
        .setContentText(eventLocation)
        .setContentIntent(viewPendingIntent)
        .addAction(R.drawable.ic_map,
                getString(R.string.map), mapPendingIntent)
        .setStyle(bigStyle);

注意你可以使用setLargeIcon方法添加一个大图片到任何一个通知中,然而这些图标以大背景图展示在wear上而且不太好看因为这些图片被放大适应wear的屏幕尺寸.添加一个特殊的图片到通知上可以参考Add Wearable Features For a Notification,更多关于设计大图标的通知可以看这里

为通知添加可穿戴的特征

如果你想为你的通知添加可穿戴设备的特征,比如指定其他内容页面或者让用户通过语音输入指定文字回复.你可以使用NotificationCompat.WearableExtender这个类.

  1. 创建一个WearableExtender实例对象,给通知设置可穿戴设备的特征.
  2. 实例化一个NotificationCompat.Builder对象,按照上面所描述设置所需属性
  3. 调用extend()方法,传递一个WearableExtender对象.这就使得通知具有可穿戴特性.
  4. 调用build()去构建通知.

举个例子,下面的代码调用setHintHideIcon()方法从通知中去掉应用程序图标.

// Create a WearableExtender to add functionality for wearables
NotificationCompat.WearableExtender wearableExtender =
        new NotificationCompat.WearableExtender()
        .setHintHideIcon(true)
        .setBackground(mBitmap);

// Create a NotificationCompat.Builder to build a standard notification
// then extend it with the WearableExtender
Notification notif = new NotificationCompat.Builder(mContext)
        .setContentTitle("New mail from " + sender)
        .setContentText(subject)
        .setSmallIcon(R.drawable.new_mail)
        .extend(wearableExtender)
        .build();

setHintHideIcon()setBackground()这两个方法仅仅是和NotificationCompat.WearableExtender有关的两个可见的通知新特性.

Note: The bitmap that you use with setBackground() should have a resolution of 400x400 for non-scrolling backgrounds and 640x400 for backgrounds that support parallax scrolling. Place these bitmap images in the res/drawable-nodpi directory. Place other non-bitmap resources for wearable notifications, such as those used with the setContentIcon() method, in the res/drawable-hdpi directory.

这点提示没有看懂,好像是说对于400*400分辨率的图不能滚动,640*400的可以滚动.

如果你以后需要使用可穿戴特性特定选项,你可以使用特定属性的get方法.下面这个例子调用getHintHideIcon()方法获取是否隐藏了通知的图标.

NotificationCompat.WearableExtender wearableExtender =
        new NotificationCompat.WearableExtender(notif);
boolean hintHideIcon = wearableExtender.getHintHideIcon();

Deliver the Notification (交付通知)

当希望交付传递一个通知时,需要用NotificationManagerCompat代替NotificationManager

// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =
        NotificationManagerCompat.from(mContext);

// Issue the notification with notification manager.
notificationManager.notify(notificationId, notif);

如果使用framework层的NotificationManager,那么一些NotificationCompat.WearableExtender的特性不能使用.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容

  • 原文出处: http://www.androidchina.net/6174.html Notification在...
    木木00阅读 12,270评论 3 32
  • 一、什么是Notification? Notification是一种有全局效果的通知,可以显示在系统通知栏。以下内...
    douhao1333阅读 724评论 0 1
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,494评论 18 139
  • 前两天看了官方的教学视频,讲的是使用NotificationCompact来使用通知.后来网上搜索了关与通知的文章...
    花京院典明阅读 4,876评论 0 5
  • 转载自:http://blog.csdn.net/vipzjyno1/article/details/252480...
    HEXG_阅读 5,779评论 0 2