0

私はAndroidアプリを開発しています。私のアプリでは、放送受信機の内部から発せられた通知を表示しています。受信者からの通知が正常に動作しています。しかし、私はそれが欲しいと少し問題があります。自己キャンセル通知です。ユーザーが閉じることはできません。今起こっているのは、それが起動されたときに、既に存在する場合は通知バーに新しい通知として追加されません。既存のものだけが表示され続けます。私が望むのは、レシーバー内の通知マネージャーからどれだけ多くの通知があっても、いつでも起動されます。下記の私のシナリオをご覧ください。 Androidのブロードキャスト受信機から起動するたびに新しいものとして追加された通知を表示するには

この

は、これは私が活動に受信機を使用して通知を発射していますどのように自己終了通知

public class GalleryImageUploadReceiver extends BroadcastReceiver { 

    private Context context; 
    private NotificationCompat.Builder notification; 
    private static final int NOTIFICATION_ID = 1; 
    private NotificationManager nm; 

    @Override 
    public void onReceive(Context pContext, Intent intent) { 
     this.context = pContext; 
     initialize(intent); 
     showProcessNotification(); 
     uploadImagesToServer(); 
    } 

    private void initialize(Intent intent) 
    { 
     //initialize data 
    } 

    private void uploadImagesToServer() 
    { 
     // do async task and close itself 
     if(nm!=null) 
     { 
      nm.cancel(NOTIFICATION_ID); 
     } 
    } 

    public void showProcessNotification() 
    { 
     try{ 

      notification = new NotificationCompat.Builder(context); 
      notification.setAutoCancel(true); 
      notification.setSmallIcon(R.mipmap.ic_launcher); 
      notification.setWhen(System.currentTimeMillis()); 
      notification.setTicker("Uploading image(s)..."); 
      notification.setContentTitle(context.getResources().getString(R.string.app_name)); 
      notification.setContentTitle("Uploading image(s)..."); 
      notification.setAutoCancel(true); 
      notification.setOngoing(true); 
      notification.setDefaults(Notification.FLAG_ONGOING_EVENT); 
      nm = (NotificationManager)context.getApplicationContext().getSystemService(context.getApplicationContext().NOTIFICATION_SERVICE); 
      nm.notify(NOTIFICATION_ID,notification.build()); 
     } 
     catch (Exception e) 
     { 

     } 


    } 


    public void uploadImages(Context context,Intent intent) 
    { 
     onReceive(context,intent); 
    } 
} 

火災私の受信機である

//inside a click listener 
GalleryImageUploadReceiver receiver = new GalleryImageUploadReceiver(); 
receiver.uploadImages(getBaseContext(),intent); 

そのコードの問題ですが、私はボタンをクリックすると、新しい通知は既存の通知よりも優先されるため、画面には1つの通知のみが表示されます。何度も何度もクリックします。たとえば、通知が最後の5秒間に発生した場合、最初のボタンが放されてから2秒後にもう一度ボタンをクリックすると、その既存のボタンが7秒間続きます。新しいものは通知バーに追加されません。ボタンをクリックするたびに、どうすれば新しいものとして追加することができますか?

答えて

1

通知ごとに一意の通知IDを使用します。 'NOTIFICATION_ID'定数を使用しているため、新しい通知は既存の通知を上書きします。

+0

ありがとうございます。完璧。 –

関連する問題