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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Song_74110/article/details/70174834

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

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);//取消消息回调
这两个就是设置正常打开消息的设置方法

参考

猜你喜欢

转载自blog.csdn.net/Song_74110/article/details/70174834
今日推荐