码迷,mamicode.com
首页 > 移动开发 > 详细

android通知栏Notification点击,取消,清除响应事件

时间:2016-10-10 20:03:46      阅读:454      评论:0      收藏:0      [点我收藏+]

标签:

主要是检测android通知栏的三种状态的响应事件

这次在实现推送需求的时候,要用到android通知栏Notification点击后进入消息页面,因为要实现一个保存推送用户名字的功能,我在点击后处理了这个功能,但是测试发现我点击删除或者滑动清除后这个功能并没有执行,所以才意识到要处理删除和滑动清除的事件:

首先实现一个BroadcastReceiver

public class NotificationBroadcastReceiver extends BroadcastReceiver {

    public static final String TYPE = "type"; //这个type是为了Notification更新信息的,这个不明白的朋友可以去搜搜,很多
    
    @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_clicked")) {
            //处理点击事件
        }
        
        if (action.equals("notification_cancelled")) {
            //处理滑动清除和点击删除事件
        }
    }
}

 

而注册Notification是这样的:

Intent intentClick = new Intent(this, NotificationBroadcastReceiver.class);
intentClick.setAction("notification_clicked");
intentClick.putExtra(NotificationBroadcastReceiver.TYPE, type);
PendingIntent pendingIntentClick = PendingIntent.getBroadcast(this, 0, intentClick, PendingIntent.FLAG_ONE_SHOT);

Intent intentCancel = new Intent(this, NotificationBroadcastReceiver.class);
intentCancel.setAction("notification_cancelled");
intentCancel.putExtra(NotificationBroadcastReceiver.TYPE, type);
PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(this, 0, intentCancel, PendingIntent.FLAG_ONE_SHOT);

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle(getString(R.string.setting_gcm_title))
        .setContentText(message)
        .setSound(defaultSoundUri)
        .setContentIntent(pendingIntentClick)
        .setDeleteIntent(pendingIntentCancel);

notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(type /* ID of notification */, notificationBuilder.build());  //这就是那个type,相同的update,不同add
 

 

当然不要忘了在AndroidManifest.xml里面注册

<receiver android:name=".gcm.NotificationBroadcastReceiver">
    <intent-filter>
        <action android:name="notification_cancelled"/>
        <action android:name="notification_clicked"/>
    </intent-filter>
</receiver>

 

这样就是初步实现了android通知栏Notification点击,取消,清除响应事件

android通知栏Notification点击,取消,清除响应事件

标签:

原文地址:http://www.cnblogs.com/qianyukun/p/5946792.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!