1
我想檢查通知是否被用戶關閉?這可能嗎?我的意思是,如果通知是通過刷卡關閉的,那麼我想爲DB設置一個值。檢查通知已關閉在android
我知道如何檢查通知是否處於活動狀態,但不知道如何通過刷卡來關閉通知?
怎麼辦?有人可以幫我嗎?
謝謝!
我想檢查通知是否被用戶關閉?這可能嗎?我的意思是,如果通知是通過刷卡關閉的,那麼我想爲DB設置一個值。檢查通知已關閉在android
我知道如何檢查通知是否處於活動狀態,但不知道如何通過刷卡來關閉通知?
怎麼辦?有人可以幫我嗎?
謝謝!
試試這可以幫助你
1)製作方法發送通知
public void sendNotification(String message, int notificationId) {
Bundle b = new Bundle();
Intent intent = new Intent(this, NotificationDismissedReceiver.class);
intent.putExtra("com.stack.notificationId", notificationId);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText(message)
.setAutoCancel(true)
.setDeleteIntent(createOnDismissedIntent(this, 11))
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
Notification note = notificationBuilder.build();
note.defaults |= Notification.DEFAULT_VIBRATE;
note.defaults |= Notification.DEFAULT_SOUND;
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId /* ID of notification */, notificationBuilder.build());
}
2)請BroadcastReciver
檢測刷卡Notification
public class NotificationDismissedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int notificationId = intent.getExtras().getInt("com.stack.notificationId");
/* Your code to handle the event here */
Log.d("TAG","GET "+notificationId);
}
}
3)創建解僱意向的方法OD
private PendingIntent createOnDismissedIntent(Context context, int notificationId) {
Intent intent = new Intent(context, NotificationDismissedReceiver.class);
intent.putExtra("com.stack.notificationId", notificationId);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(context.getApplicationContext(),
notificationId, intent, PendingIntent.FLAG_ONE_SHOT);
return pendingIntent;
}
4)manifest
<receiver
android:name=".NotificationDismissedReceiver"
android:exported="false" >
</receiver>
5)調用的方法添加BroadcastReciver
的通知
sendNotification("hello world",11);
太棒了。謝謝!如何檢查通知是否已被點擊?這可能嗎? –
嗨檢查這個帖子http://stackoverflow.com/ q/14671453/2445061 – ChrisCarneiro
看來你正在尋找[刪除Intent](https://developer.android.com/reference/android/app/Notification.html#deleteIntent)字段的'Notification'類。 – Lawrance