前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android Notification的用法

Android Notification的用法

作者头像
码客说
发布2019-10-22 14:16:35
6100
发布2019-10-22 14:16:35
举报
文章被收录于专栏:码客码客

旧版本

api11中废弃(Android 3.0)

代码语言:javascript
复制
String service = NOTIFICATION_SERVICE;
nManager = (NotificationManager) this.getSystemService(service);

notification = new Notification();
// 通知提示
String tickerText = "状态栏上显示的消息"; 
// 显示时间
long when = System.currentTimeMillis();
notification.icon = R.drawable.icon;// 设置通知的图标
notification.tickerText = tickerText; // 显示在状态栏中的文字
notification.when = when; // 设置来通知时的时间

notification.ledARGB = 0xff00ff00;
// 点击清除按钮或点击通知后会自动消失
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// 设置默认铃声
notification.defaults = Notification.DEFAULT_SOUND;
// 设置铃声震动
notification.defaults = Notification.DEFAULT_ALL; 

// 单击通知后会跳转到NotificationResult类
Intent intent = new Intent(this,MainActivity.class);
// 获取PendingIntent,点击时发送该Intent
PendingIntent  pIntent = PendingIntent.getActivity(this, 0, intent, 0);
// 设置通知的标题和内容
notification.setLatestEventInfo(this, "消息的标题","消息的内容", pIntent);
// 发出通知
nManager.notify(1, notification);

新版本

api版本>=16(Android 4.1)

代码语言:javascript
复制
String service = NOTIFICATION_SERVICE;
nManager = (NotificationManager) this.getSystemService(service);
Notification.Builder builder = new Notification.Builder(this);
builder.setTicker("状态栏上显示的消息");
builder.setContentTitle("消息的标题");
builder.setContentText("消息的内容");
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setAutoCancel(true);
builder.setWhen(System.currentTimeMillis());

Intent intent = new Intent(this,MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(pendingIntent);

Notification notification = builder.build();
nManager.notify(1, notification);
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-03-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 旧版本
  • 新版本
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档