1

ここで私がスタックオーバーフローで見つけたいくつかの回答を使用しましたが、動作しませんでした。しかし、基本的には、私は2つのことをするように通知する必要があります。 1つは、通知自体がクリックされたときに再びアプリケーションを開く必要があるため、AddActionがクリックされたときに通知を閉じる必要があります。Androidの通知がAddActionで終了しないをクリック

通知は、クリックされたときにアプリケーションを開きますが、これは正しいですが、AddAction(「完了」)をクリックすると、同じことが行われます。通知を閉じるアクションの代わりに、通知自体と同様にアプリを開く。何がうまくいかないでしょうか?

public void onInput(MaterialDialog dialog, CharSequence input) { 

    //notification body 
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext()); 
     PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, 
      new Intent(getApplicationContext(), MainActivity.class) 
        .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP), 
      0); 

     //Rest of Notification 
     builder.setStyle(new NotificationCompat.BigTextStyle().bigText(input.toString())); //BigText 
     builder.setOngoing(true); //Make persistent 
     builder.setContentIntent(pendingIntent); //OnClick for Reopening App 
     builder.setSmallIcon(R.drawable.ic_note); 
     builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); 
     builder.setContentTitle("Remember!"); 
     builder.setContentText(input.toString()); //Get text from dialog input 
     Intent closeIntent = new Intent(getApplicationContext(), MainActivity.class); 
     closeIntent.putExtra(getPackageName(), NOTIFICATION_ID); 
     PendingIntent closeBtn = PendingIntent.getActivity(getApplicationContext(), 0, closeIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
     builder.addAction(R.drawable.ic_action_name, "Done", closeBtn); //Action for the closer 
     notificationManager.notify(NOTIFICATION_ID, builder.build()); 

    //toast 
    Toast.makeText(MainActivity.this, "Done! Reminder has been set. Check your Notification Bar! :)", 
      Toast.LENGTH_LONG).show(); 

    //Close app when done entering in text 
    finish(); 
} 

答えて

0

は単にbuilder.autoCancel(true);

を追加これはあなたの問題を解決します。

0

あなたが持っているこのコード:

Intent closeIntent = new Intent(getApplicationContext(), MainActivity.class); 
closeIntent.putExtra(getPackageName(), NOTIFICATION_ID); 
PendingIntent closeBtn = PendingIntent.getActivity(getApplicationContext(), 0, 
     closeIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
builder.addAction(R.drawable.ic_action_name, "Done", 
     closeBtn); //Action for the closer 

は(あなたのアプリを起動する)closeIntentstartActivity()を呼び出します "完了" と表示されたアクションを、置きます。それはまさにそれがすべきことです。

ユーザーは何もせずに通知をスワイプして削除する方法を既に知っています。なぜそれを達成するために通知に追加のアクションボタンを追加するのはどうですか?私は過労だと思う。

あなたはこれをしたいreall場合は、基本的には、ユーザーがボタンをクリックしたときに何も起こりませんしたいので、アクションにヌルPendingIntentを使用するように試みることができる:

builder.addAction(R.drawable.ic_action_name, "Done", 
     null); //Action for the closer 
+0

私がボタンを追加したのは、通知が永続的で、決してスワイプできないためです。これは、通知を永続的に保ちながら動作させる方法を見つけられなかったので、私が問題に取り組んでいるところです – MJonesDev

関連する問題