1

私はandroidアプリを開発しようとしています。このアプリではチャット機能を実装しています。 ですが、メッセージを受け取ったときに通知音が鳴ります。 ユーザーがアプリを使用しているときに通知を停止するにはアプリの実行中にアンドロイドでプログラムで通知をオフにする方法はありますか?

private void ChatNotifications(String uid, String fuid, String message) { 
      NotificationCompat.BigTextStyle notiStyle = new NotificationCompat.BigTextStyle(); 
      notiStyle.setBigContentTitle("message"); 
      // notiStyle.setSummaryText(quote); 
      notiStyle.bigText(message); 
      Intent resultIntent = new Intent(this, Loading.class); 

      resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
      stackBuilder.addParentStack(Loading.class); 
      stackBuilder.addNextIntent(resultIntent); 
      PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 
      //Uri defaultSoundUri1 = Uri.parse("android.resource://com.therevew.quotes/" + R.raw.hello); 
      Uri defaultSoundUri1= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
      Notification myNotification = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.common_google_signin_btn_icon_dark) 
        // .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ii_launcher)) 
        .setSound(defaultSoundUri1) 
        .setAutoCancel(true) 
        .setColor(getResources().getColor(R.color.notificationColor)) 
        .setContentIntent(resultPendingIntent) 
        .setContentTitle("message") 
        .setContentText(message) 
        .setStyle(notiStyle).build(); 
      NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
      notificationManager.notify(101/* ID of notification */, notiStyle.build()); 
     } 
+0

を必要とします質問を投稿する前にSO上でインターネットや前の質問の上にいくつかの研究を行ってください。 – MobileEvangelist

答えて

0

あなたがどんな音を必要としないときだけ

Notification myNotification = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.common_google_signin_btn_icon_dark) 
        // .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ii_launcher)) 
        .setSound(defaultSoundUri1) 
        .setAutoCancel(true) 
        .setColor(getResources().getColor(R.color.notificationColor)) 
        .setContentIntent(resultPendingIntent) 
        .setContentTitle("message") 
        .setContentText(message) 
        .setStyle(notiStyle).build(); 

から

.setSound(defaultSoundUri1) 

を削除します。 これは、これを行うにしてみてください、あなたを助けにはなりません場合は、次の

Notification myNotification = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.common_google_signin_btn_icon_dark) 
        // .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ii_launcher)) 
        .setSound(null) 
        .setAutoCancel(true) 
        .setColor(getResources().getColor(R.color.notificationColor)) 
        .setContentIntent(resultPendingIntent) 
        .setContentTitle("message") 
        .setContentText(message) 
        .setStyle(notiStyle).build(); 
myNotification.defaults = 0; 
+0

**これを削除する場合** setSound(defaultSoundUri1)**通知停止作業 は永久に。 –

3

最初の部分を

「それは通知音を作るのメッセージ」です。

最初にアプリケーションがフォアグラウンドになっているかどうかをチェックし、そうであれば、サウンドの使用を停止するset defaults to 0 and sound to null。ユーザーがプログラムのアプリ を使用しているとき、私は通知を停止することができますどのようにこの.setSound(isInForeGround ? null : uri).

のようなメソッドのparamsを変更しますか?

これはあなたが従うべきだと思います。

ユーザーあたりの経験はあなたが通知を止めるべきではない懸念しているとして、 あなただけのユーザーが、アプリはnotificationBarで開いているときにそれらがアップpilled を見ることはできないはずな方法でそれらを消費する必要があります。

チャットアプリケーションでは、 チャンネルを使用して、2人のユーザーがオンラインになっているときに受信メッセージを送信し、メッセージを受信したときに とチャットする必要があります。

したがって、noticiationBarで特定のチャットの受信データを から保留中の意図を作成しないでください。

これはコンセプト消費通知をクリアします。お役に立てれば。

+0

@ deepak-mittalあなたの問題を解決する場合は、回答を受け入れたものとしてマークしてください。 – MobileEvangelist

+0

私はどのようにデフォルト値を変更していますか? –

+0

@ deepak-mittalアプリケーションがフォアグラウンドにあるかどうかを確認し、このメソッドの条件演算子を使用します.setSound(isinForeground?null:uri)。これはあなたの問題を解決します。フォアグラウンドのアプリケーションでは、最良のアプローチを得るためにいくつかのインターネットリサーチを行うことをお勧めします。これが役に立ったら – MobileEvangelist

1
//if you use fcm then try  

    @Override 
    public void onMessageReceived(final RemoteMessage remoteMessage) { 
     if (remoteMessage.getNotification() != null) { 
      String message = remoteMessage.getNotification().getBody(); 
      if (isForeground(getApplicationContext())) { 
       //if in forground then your operation 
      // if app is running them 
      } else { 
      //if in background then perform notification operation 
       sendNotification(message); 
      } 
     } 
    } 


     private static boolean isForeground(Context context) { 
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 
        List<ActivityManager.RunningAppProcessInfo> tasks = am.getRunningAppProcesses(); 
        final String packageName = context.getPackageName(); 
        for (ActivityManager.RunningAppProcessInfo appProcess : tasks) { 
         if (ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND == appProcess.importance && packageName.equals(appProcess.processName)) { 
          return true; 
         } 
        } 
        return false; 
       } 
+1

私のための完全な助け..と感謝@vasupujy –

0

まずAndroidManifest に

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/> 

を設定し、この場所は

final AudioManager mode = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE); 
     mode.setRingerMode(AudioManager.RINGER_MODE_SILENT); 
関連する問題