2
这里我显示从Service
类的Android推送通知,这是显示罚款和获得解雇,如果我们点击完整Remoteview
但当我打开Activity
从RemoteView
的按钮/文本中,通知不会被取消。取消不工作在活动取消特定的推送通知android
在服务:
Intent notificationRecordFollowupintent = new Intent(this, RecordFollowUpActivity.class);
notificationRecordFollowupintent.putExtra("notify_id", m);
PendingIntent contentRecordFollowupIntent = PendingIntent.getActivity(this, m, notificationRecordFollowupintent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
builder.setSmallIcon(R.mipmap.ic_launcher)
.setWhen(System.currentTimeMillis())
.setDefaults(NotificationCompat.DEFAULT_SOUND)
.setStyle(bigTextStyle)
.setCustomBigContentView(contentView)
.setCustomContentView(contentView)
.setGroup("Follow Up Alert")
.setNumber(++numMessages)
.setOngoing(true)
.setAutoCancel(true)
.setContentIntent(contentRecordFollowupIntent);
} else {
builder.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Follow Up Alert")
.setContentText("" + name)
.setWhen(System.currentTimeMillis())
.setDefaults(NotificationCompat.DEFAULT_SOUND)
.setStyle(bigTextStyle)
.setCustomContentView(contentView)
.setCustomBigContentView(contentView)
.setContentIntent(contentRecordFollowupIntent)
.setAutoCancel(true);
}
Notification notification = builder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(getResources().getString(R.string.app_name), m, notification);
在活动时间:
Intent intent = getIntent();
int notificationId = intent.getIntExtra("notify_id", -1);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(notificationId);
我得到相同的ID也仍然不会被取消。
你可以分享你的代码来创建通知吗?它在服务中是否使用与变量'm'相同的ID?您正在使用'm'作为PendingIntent的'requestCode',但是我看不到'm'是否被用于'notificationManager.notify()'上的实际'notificationId'。 – Grimthorr
当然,检查我更新的问题 –
谢谢,这似乎应该工作!服务如何决定何时创建通知?它可以重新创建它比它可以取消更快吗? – Grimthorr