Một Idea:
i f bạn tạo một thông báo bình thường, bạn cũng cần các hành động một, hai hoặc 3 trong số chúng. Tôi đã tạo ra một "NotifyManager" nó tạo ra tất cả các thông báo tôi cần và cũng nhận được tất cả các cuộc gọi Intent. Vì vậy, tôi có thể quản lý tất cả các hành động VÀ cũng bắt sự kiện sa thải tại MỘT địa điểm.
public class NotifyPerformService extends IntentService {
@Inject NotificationManager notificationManager;
public NotifyPerformService() {
super("NotifyService");
...//some Dagger stuff
}
@Override
public void onHandleIntent(Intent intent) {
notificationManager.performNotifyCall(intent);
}
để tạo ra các deleteIntent sử dụng này (trong NotificationManager):
private PendingIntent createOnDismissedIntent(Context context) {
Intent intent = new Intent(context, NotifyPerformMailService.class).setAction("ACTION_NOTIFY_DELETED");
PendingIntent pendingIntent = PendingIntent.getService(context, SOME_NOTIFY_DELETED_ID, intent, 0);
return pendingIntent;
}
và mà tôi sử dụng để thiết lập tiếp cận mục đích xóa như thế này (trong NotificationManager):
private NotificationCompat.Builder setNotificationStandardValues(Context context, long when){
String subText = "some string";
NotificationCompat.Builder builder = new NotificationCompat.Builder(context.getApplicationContext());
builder
.setLights(ContextUtils.getResourceColor(R.color.primary) , 1800, 3500) //Set the argb value that you would like the LED on the device to blink, as well as the rate
.setAutoCancel(true) //Setting this flag will make it so the notification is automatically canceled when the user clicks it in the panel.
.setWhen(when) //Set the time that the event occurred. Notifications in the panel are sorted by this time.
.setVibrate(new long[]{1000, 1000}) //Set the vibration pattern to use.
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
.setSmallIcon(R.drawable.ic_white_24dp)
.setGroup(NOTIFY_GROUP)
.setContentInfo(subText)
.setDeleteIntent(createOnDismissedIntent(context))
;
return builder;
}
và cuối cùng trong cùng một NotificationManager là chức năng thực hiện:
public void performNotifyCall(Intent intent) {
String action = intent.getAction();
boolean success = false;
if(action.equals(ACTION_DELETE)) {
success = delete(...);
}
if(action.equals(ACTION_SHOW)) {
success = showDetails(...);
}
if(action.equals("ACTION_NOTIFY_DELETED")) {
success = true;
}
if(success == false){
return;
}
//some cleaning stuff
}
Hoạt động :) thankyou rất nhiều! –
Điều này là muộn. Tôi chỉ tự hỏi nếu có một cách tiếp cận tương tự cho các thông báo có 'builder.setAutoCancel (true);' bởi vì khi người dùng nhấp vào thông báo và nó bị hủy, xóa-Intent không được kích hoạt –
@dev_android checkout http://developer.android.com/reference/android/app/Notification.Builder.html#setContentIntent(android.app.PendingIntent) –