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。改變方法PARAMS這樣.setSound(isInForeGround ? null : uri).

如何停止的通知,當用戶使用的應用程序編程 ?

我想這是你應該遵循的;

按照用戶體驗來講,你不應該停止的通知, 你只需要使用它們的方式,用戶不應該能夠看到 他們起球了,當應用程序在notificationBar開放。

作爲聊天應用程序而言,您應該使用 頻道發送接收消息,如果兩個用戶在線並且在您收到消息時聊天 。

所以只是不要從 創建待處理的意圖在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

首先設置

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

在AndroidManifest 然後做這在需要

final AudioManager mode = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE); 
     mode.setRingerMode(AudioManager.RINGER_MODE_SILENT); 
相關問題