2015-12-26 61 views
1

我想檢測時留下通知用戶刷卡 - 它可以在任何通知,因爲我會檢測該通知使用通知監聽最近駁回。檢測向左滑動在通知欄中?

是否有一個「全球性」的姿態刷卡,我可以聽,只有當我發現我的通知,駁回觸發我的應用程序特定的事件?

+2

AFAIK,這是不可能的,除非是通過自定義ROM。 – CommonsWare

+2

不可能的,但是,你可以使用通知按鈕 – Nirel

回答

1

嘗試以下

1)創建一個接收器來處理輕掃到解僱事件:

public class NotificationDismissedReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     int notificationId = intent.getExtras().getInt("com.my.app.notificationId"); 
     /* Your code to handle the event here */ 
    } 
}`enter code here` 
2) Add an entry to your manifest: 

<receiver 
    android:name="com.my.app.receiver.NotificationDismissedReceiver" 
    android:exported="false" > 
</receiver> 
3) Create the pending intent using a unique id for the pending intent (the notification id is used here) as without this the same extras will be reused for each dismissal event: 

private PendingIntent createOnDismissedIntent(Context context, int notificationId) { 
    Intent intent = new Intent(context, NotificationDismissedReceiver.class); 
    intent.putExtra("com.my.app.notificationId", notificationId); 

    PendingIntent pendingIntent = 
      PendingIntent.getBroadcast(context.getApplicationContext(), 
             notificationId, intent, 0); 
    return pendingIntent; 
} 
4) Build your notification: 

Notification notification = new NotificationCompat.Builder(context) 
       .setContentTitle("My App") 
       .setContentText("hello world") 
       .setWhen(notificationTime) 
       .setDeleteIntent(createOnDismissedIntent(context, notificationId)) 
       .build(); 

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
notificationManager.notify(notificationId, notification); 
+0

非常有用!這就是我一直在尋找的精確解:) –