サービスが完了した(成功または失敗した)ユーザーに警告するためにアンドロイド通知を使用しています。処理が完了したらローカルファイルを削除します。スワイプをキャッチしてイベントを終了する
私の問題は、障害が発生した場合、ユーザーに「再試行」オプションを許可したいということです。再試行しないことを選択し、通知を却下する場合は、処理目的で保存されたローカルファイル(画像など)を削除します。
通知のスワイプ/解除イベントをキャッチする方法はありますか?
サービスが完了した(成功または失敗した)ユーザーに警告するためにアンドロイド通知を使用しています。処理が完了したらローカルファイルを削除します。スワイプをキャッチしてイベントを終了する
私の問題は、障害が発生した場合、ユーザーに「再試行」オプションを許可したいということです。再試行しないことを選択し、通知を却下する場合は、処理目的で保存されたローカルファイル(画像など)を削除します。
通知のスワイプ/解除イベントをキャッチする方法はありますか?
DeleteIntent: DeleteIntent通知に関連付けることができ、通知が削除されるとき、エーテルにより、解雇ますPendingIntentオブジェクトです:
ブロードキャストレシーバに保留中のインテントを設定し、必要な操作を実行できます。
1)スワイプ対を処理するために受信機を作成します。
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0);
Builder builder = new Notification.Builder(this):
..... code for your notification
builder.setDeleteIntent(pendingIntent);
MyBroadcastReceiver
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
.... code to handle cancel
}
}
Aは完全に(答えのために氏は私のおかげで)答えを洗い流しますイベントを却下する:
public class NotificationDismissedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int notificationId = intent.getExtras().getInt("com.my.app.notificationId");
/* Your code to handle the event here */
}
}
2)あなたにエントリを追加する
private PendingIntent createOnDismissedIntent(Context context, int notificationId) {
Intent intent = new Intent(context, NotificationDismissedReceiver.class);
intent.putExtra("com.my.app.notificationId", notificationId);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(context.getApplicationContext(),
notificationId, intent, 0);
return pendingIntent;
}
:Rマニフェスト:同じエクストラ各解雇イベントのために再利用され、このことなくとして
<receiver
android:name="com.my.app.receiver.NotificationDismissedReceiver"
android:exported="false" >
</receiver>
3)は、(通知IDをここで使用されている)、保留中の目的のための固有のIDを使用して、保留中の意図を作成します
4)あなたの通知をビルドします。
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle("My App")
.setContentText("hello world")
.setWhen(notificationTime)
.setDeleteIntent(createOnDismissedIntent(context, notificationId))
.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, notification);
素敵なやつ! –
私のために働かなかった、常にエラー "受信者をインスタンス化することができませんでした...."引数のないコンストラクタがありません。 もう1つの同様のソリューションを実装した後で、ブロードキャストレシーバの登録で解決しました: http://stackoverflow.com/questions/13028122/how-to-use-delete-intent-to-perform-some-action-on -clear-notification –
これは私のために働きますが、通知をクリックするとイベントを呼び出すことはできません。クリックイベントを聞くことはできますか? –
もう一つのアイデア:
I通常、通知は1つ、2つまたは3つのアクションが必要です。私は必要なすべての通知を作成し、すべてのインテントコールを受信する「NotifyManager」を作成しました。 私はすべてのアクションを管理することができますまた、1つの場所で棄権イベントをキャッチします。
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;
}
と私は(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;
}
(NotificationManagerで)これを使用deleteIntentを作成する
public class NotifyPerformService extends IntentService {
@Inject NotificationManager notificationManager;
public NotifyPerformService() {
super("NotifyService");
...//some Dagger stuff
}
@Override
public void onHandleIntent(Intent intent) {
notificationManager.performNotifyCall(intent);
}
最後に同じNotificationManagerにperform関数があります。
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
}
作品:)ありがとう! –
これは遅れています。 'builder.setAutoCancel(true);' ユーザが通知をクリックして取り消され、削除インテントがトリガーされないため、同様のアプローチがあるかどうか疑問に思っていただけです。 –
@dev_android checkout http:///developer.android.com/reference/android/app/Notification.Builder.html#setContentIntent(android.app.PendingIntent) –