android通知欄Notification點擊,取消,清除響應事件

來源:互聯網
上載者:User

標籤:

主要是檢測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點擊,取消,清除響應事件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.