检查通知已在android中关闭
我想检查用户是否关闭了通知?这可能吗?我的意思是,如果通过滑动关闭通知,那么我希望将值设置为DB 我知道如何检查通知是否处于活动状态,但不确定如何通过刷卡关闭通知 怎么做?有人能帮我吗检查通知已在android中关闭,android,notifications,android-notifications,Android,Notifications,Android Notifications,我想检查用户是否关闭了通知?这可能吗?我的意思是,如果通过滑动关闭通知,那么我希望将值设置为DB 我知道如何检查通知是否处于活动状态,但不确定如何通过刷卡关闭通知 怎么做?有人能帮我吗 谢谢 试试这个,它可能对你有帮助 1)生成发送通知的方法 public void sendNotification(String message, int notificationId) { Bundle b = new Bundle(); Intent intent = new Intent(t
谢谢 试试这个,它可能对你有帮助 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());
}
sendNotification("hello world",11);
2)制作广播接收器
用于检测刷卡通知
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);
}
}
<receiver
android:name=".NotificationDismissedReceiver"
android:exported="false" >
</receiver>
3)创建消除意图方法
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
中添加broadcastReceiver
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);
}
}
<receiver
android:name=".NotificationDismissedReceiver"
android:exported="false" >
</receiver>
您好,查看这篇文章,您似乎正在查找
通知
类的字段。太好了。谢谢如何检查通知是否已单击?这可能吗?@SanjanaNair你得到答案了吗?我也需要帮助。