取消通知后,不能再次通知同一个ID的通知。为什么?
After cancel a notification, cannot renotify a notification with the same ID again. Why?
我正在 Android Studio 上开发。我有一项服务会生成通知,当在网络(WiFi、3G)中检测到更改时,该通知会更新。该服务由开关控制,并生成用户无法删除的通知。
当我停止服务时,我使用 cancel(id) 删除通知。
问题是当我再次尝试激活该服务时,没有生成通知。有没有什么办法解决这一问题?
这些是我生成通知的代码行:
PD。没有产生异常
编辑
我试图用另一个 ID 创建通知,但它不起作用。
protected void onProgressUpdate(String... values) {
WifiManager wifimanager = (WifiManager) context.getSystemService(WIFI_SERVICE);
//Datos
try {
if(wifimanager.isWifiEnabled() && values[0].equalsIgnoreCase("false") && values[1].equalsIgnoreCase("true")) {
NotificationManager manager;
Notification myNotication;
manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
Intent notificacionIntent = new Intent(context.getApplicationContext(), Servicio.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, notificacionIntent, 0);
Notification.Builder builder = new Notification.Builder(context);
builder.setTicker("Wifi-3G");
builder.setContentTitle("Wifi-3G Notification");
builder.setContentText(getString(R.string.conectado_wifi));
builder.setSmallIcon(R.mipmap.logo_agus_android);
builder.setContentIntent(pendingIntent);
builder.setOngoing(true);
//builder.setAutoCancel(true);
myNotication = builder.build();
manager.notify(11, myNotication);
}
这里我取消服务:
swt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
DatabaseHandler db = new DatabaseHandler(Servicio.this);
auxS = Boolean.toString(isChecked);
Preferencia pref = new Preferencia(2,"Servicio", auxS);
db.updatePrefe(pref);
if (isChecked){
startService(new Intent(Servicio.this, MyService.class));
Notificacion noti = new Notificacion(1, 11, "Wifi-3G Notification", "Estás conectado a la red a través de WiFi", "Servicio.class");
db.addNotificacion(noti);
}
else {
stopService(new Intent(Servicio.this, MyService.class));
Notificacion noti = new Notificacion(1, 11, "Wifi-3G Notification", "Estás conectado a la red a través de WiFi", "Servicio.class");
db.deleteNotificacion(noti);
}
}
});
我在onDestroy()方法中取消通知:
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, R.string.servicio_destruido, Toast.LENGTH_SHORT).show();
DatabaseHandler db = new DatabaseHandler(this);
pref=new Preferencia(3,"EstadoServ","false");
db.updatePrefe(pref);
NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
manager.cancel(11);
Conexiones.cancel(true);
}
我不会说英语,这是我的第一个问题,抱歉。如果您需要有关我的代码的更多信息,请随时问我。
您可以更新通知,但您不能使用与内存中的应用程序相同的通知ID。
解决,问题是没有使用onCancelled()方法
我正在 Android Studio 上开发。我有一项服务会生成通知,当在网络(WiFi、3G)中检测到更改时,该通知会更新。该服务由开关控制,并生成用户无法删除的通知。 当我停止服务时,我使用 cancel(id) 删除通知。 问题是当我再次尝试激活该服务时,没有生成通知。有没有什么办法解决这一问题? 这些是我生成通知的代码行: PD。没有产生异常
编辑
我试图用另一个 ID 创建通知,但它不起作用。
protected void onProgressUpdate(String... values) {
WifiManager wifimanager = (WifiManager) context.getSystemService(WIFI_SERVICE);
//Datos
try {
if(wifimanager.isWifiEnabled() && values[0].equalsIgnoreCase("false") && values[1].equalsIgnoreCase("true")) {
NotificationManager manager;
Notification myNotication;
manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
Intent notificacionIntent = new Intent(context.getApplicationContext(), Servicio.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, notificacionIntent, 0);
Notification.Builder builder = new Notification.Builder(context);
builder.setTicker("Wifi-3G");
builder.setContentTitle("Wifi-3G Notification");
builder.setContentText(getString(R.string.conectado_wifi));
builder.setSmallIcon(R.mipmap.logo_agus_android);
builder.setContentIntent(pendingIntent);
builder.setOngoing(true);
//builder.setAutoCancel(true);
myNotication = builder.build();
manager.notify(11, myNotication);
}
这里我取消服务:
swt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
DatabaseHandler db = new DatabaseHandler(Servicio.this);
auxS = Boolean.toString(isChecked);
Preferencia pref = new Preferencia(2,"Servicio", auxS);
db.updatePrefe(pref);
if (isChecked){
startService(new Intent(Servicio.this, MyService.class));
Notificacion noti = new Notificacion(1, 11, "Wifi-3G Notification", "Estás conectado a la red a través de WiFi", "Servicio.class");
db.addNotificacion(noti);
}
else {
stopService(new Intent(Servicio.this, MyService.class));
Notificacion noti = new Notificacion(1, 11, "Wifi-3G Notification", "Estás conectado a la red a través de WiFi", "Servicio.class");
db.deleteNotificacion(noti);
}
}
});
我在onDestroy()方法中取消通知:
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, R.string.servicio_destruido, Toast.LENGTH_SHORT).show();
DatabaseHandler db = new DatabaseHandler(this);
pref=new Preferencia(3,"EstadoServ","false");
db.updatePrefe(pref);
NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
manager.cancel(11);
Conexiones.cancel(true);
}
我不会说英语,这是我的第一个问题,抱歉。如果您需要有关我的代码的更多信息,请随时问我。
您可以更新通知,但您不能使用与内存中的应用程序相同的通知ID。
解决,问题是没有使用onCancelled()方法