Notification的滑动清除和点击删除事件

项目里面引用了友盟的推送统计,需要统计消息的打开数量和忽略数量

Notification的属性介绍

audioStreamType 当声音响起时,所用的音频流的类型
contentIntent 当通知条目被点击,就执行这个被设置的Intent
contentView 当通知被显示在状态条上的时候,同时这个被设置的视图被显示
defaults 指定哪个值要被设置成默认的
deleteIntent 当用户点击”Clear All Notifications”按钮区删除所有的通知的时候,这个被设置的Intent被执行
icon 状态条所用的图片
iconLevel 假如状态条的图片有几个级别,就设置这里
ledARGB LED灯的颜色
ledOffMS LED关闭时的闪光时间(以毫秒计算)
ledOnMS LED开始时的闪光时间(以毫秒计算)
number 这个通知代表事件的号码
sound 通知的声音
tickerText 通知被显示在状态条时,所显示的信息
vibrate 振动模式
when 通知的时间戳

上面我加粗的字体就是正常点击传进去的PendingIntent和删除消息回调的PendingIntent

接收滑动清除和点击清除事件的回调

  • 先注册一个广播接收者
public class NotificationBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        int type = intent.getIntExtra("type", -1);

        if (type != -1) {
            NotificationManager notificationManager =
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.cancel(type);
        }

        if (action.equals("notification_cancelled")) {
            //处理滑动清除和点击删除事件

        }

    }
}
  • 在manifest里面配置
        <receiver android:name="NotificationBroadcastReceiver所在的路径">
            <intent-filter>
                <action android:name="notification_cancelled" />
            </intent-filter>

        </receiver>
  • 显示通知的代码
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setAutoCancel(true);

        //正常通知点击会跳转到MainActivity intent携带的参数可以自己获取
        Intent intentClick = new Intent(this, MainActivity.class);
        intentClick.putExtra("title", "通知标题");
        intentClick.putExtra("message", "通知内容。。。。。。");
        PendingIntent pendingIntentClick = PendingIntent.getBroadcast(this, 0,
                intentClick, PendingIntent.FLAG_ONE_SHOT);

        //滑动清除和点击删除事件
        Intent intentCancel = new Intent(this,NotificationBroadcastReceiver.class);
        intentCancel.setAction("notification_cancelled");
        intentCancel.putExtra("type", 3);
        intentCancel.putExtra("message","message");
        PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(this,0,
                intentCancel,PendingIntent.FLAG_ONE_SHOT);


        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("message title")
                .setContentText("message content")
                .setContentIntent(pendingIntentClick)//正常点击
                .setDeleteIntent(pendingIntentCancel);//取消消息回调

        mNotificationManager.notify(103, notificationBuilder.build());
setContentIntent(pendingIntentClick)//正常点击
setDeleteIntent(pendingIntentCancel);//取消消息回调
这两个就是设置正常打开消息的设置方法

参考

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值