Android消息推送(广播机制)+通知
Android广播机制使用了观察着模式;
(1) 通知
1) 获取状态通知栏管理
NotificationManager 是一个系统Service,所以必须通过getSystemService(NOTIFICATION_SERVICE)方法来获取。
即:
notificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
2) 实例化通知栏构造器NotificationCompat.Builder
Notification.Builder notificationBuilder = new Notification.Builder($.getContext())
.setSmallIcon(R.drawable.notification_app_icon) // 设置状态栏中的小图片,尺寸一般建议在24×24,这个图片同样也是在下拉状态栏中所显示,如果在那里需要更换更大的图片,可以使用setLargeIcon(Bitmap
.setPriority(Notification.PRIORITY_MAX)
.setTicker(message)// 设置在status
.setContentTitle(title)// 设置在下拉status
.setContentText($.util.strCut(message, 100))
.setNumber(1); // 在TextView的右方显示的数字,可放大图片看,在最右侧。这个number同时也起到一个序列号的左右,如果多个触发多个通知(同一ID),可以指定显示哪一个。
3) 设置PendingIntent
Intent intent = new Intent($.getContext(), LaunchActivity.class);
intent.putExtra(LaunchActivity.BUNDLE_EXTRA_NOTIFICATION, bundle);
PendingIntent pendingIntent2 = PendingIntent.getActivity($.getContext(), 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
notificationBuilder.setContentIntent(pendingIntent2);
这里的PendingIntent表示的是就是启动APP,而FLAG_CANCEL_CURRENT 表示相应的PendingIntent已经存在,则取消前者,然后创建新的PendingIntent。
4) 设置铃声和震动只执行一次
Notification notification =builder.build();
notification.flags =Notification.FLAG_ONLY_ALERT_ONCE;
这里的Notification.FLAG_ONLY_ALERT_ONCE就是表示的铃声和震动只执行一次。
更多相关文章
- Android之selector
- android Gridview生成程序快捷键的复杂方法
- 图片背景平铺
- android 控件状态配置文件
- Android(安卓)几种图像特效处理的集锦
- Android中几种图片特效的处理的实现方法
- Android中实现输入图片地址浏览图片操作
- Android通过Movie展示Gif格式图片
- android 设置开机不自动进入锁屏状态