【Android】監聽Notification被清除

來源:互聯網
上載者:User

標籤:


前言

一般非常駐的Notification是可以被使用者清除的,如果能監聽被清除的事件就可以做一些事情,比如推送重新計數的問題。

 

聲明歡迎轉載,但請保留文章原始出處:) 部落格園:http://www.cnblogs.com農民伯伯: http://over140.cnblogs.com  

 

本文

    private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent == null || context == null) {
                return;
            }

            mNotificationManager.cancel(NOTIFICATION_ID_LIVE);

            String type = intent.getStringExtra(PUSH_TYPE);
            if (PUSH_TYPE_LINK.equals(type)) {
                //mNumLinkes = 0;
            } else if (PUSH_TYPE_LIVE.equals(type)) {
                //mNumLives = 0;
            }
            //這裡可以重新計數
        }
    };


    private void sendLiveNotification() {
        Intent intent = new Intent(NOTIFICATION_CLICK_ACTION);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

        String title = "Push測試";
        mBuilder.setContentTitle(title);
        mBuilder.setTicker(title);
        mBuilder.setContentText("https://233.tv/over140");
        mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
        mBuilder.setSmallIcon(R.drawable.ic_action_cast);
        mBuilder.setDefaults(Notification.DEFAULT_ALL);
        mBuilder.setWhen(System.currentTimeMillis());
        mBuilder.setContentIntent(PendingIntent.getBroadcast(this, NOTIFICATION_ID_LIVE, intent, 0));
        mBuilder.setDeleteIntent(PendingIntent.getBroadcast(this, NOTIFICATION_ID_LIVE, new Intent(NOTIFICATION_DELETED_ACTION).putExtra(PUSH_TYPE, PUSH_TYPE_LIVE), 0));

        mNotificationManager.notify(NOTIFICATION_ID_LIVE, mBuilder.build());
    }

 代碼說明 

1、最重要的是setDeleteIntent,這個在API Level 11(Android 3.0) 新增的

2、注意不要設定setAutoCancel為true,否則監聽器接收不到

3、這裡統一了點擊通知和消除通知的操作

4、注意廣播在推送前要註冊好

 

結束

好久不寫部落格就不找借口啦

【Android】監聽Notification被清除

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.