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就是表示的铃声和震动只执行一次。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值