2017-03-11 7 views
0

私はアンドロイドアプリでプッシュ通知を取得するためにfcmを使用しています。アプリが開いたり閉じたり、状態が最小化されたときに通知を受け取っています。問題は、アプリケーションが最小化または閉じた状態で通知音が鳴らないことです。アプリが終了または最小化されたときにプッシュ通知が音が出ない

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { 


@Override 
public void onTokenRefresh() { 
    String refrehedToken = FirebaseInstanceId.getInstance().getToken(); 
    Log.d("refreshedtoken",refrehedToken+""); 
    PrefUtils.saveToPrefs(getApplicationContext(),PrefUtils.DEVICETOKEN,refrehedToken); 
    sendRegistrationToServer(refrehedToken); 

} 

private void sendRegistrationToServer(String refrehedToken) { 

} 

} 

///////////////////////////////////////////////////////////////////// ////////////////////////

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

private static final String TAG = "MyFirebaseMsgService"; 

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 

    if(remoteMessage.getData().size()>0){ 
     Log.d(TAG,"From:"+remoteMessage.getData()+""); 
     Gson gson = new Gson(); 
     String json = gson.toJson(remoteMessage.getData()); 
     Log.d("jsonresponce",json+",,,"); 
     try { 
      JSONObject object = new JSONObject(json); 
      String title = object.getString("title"); 
      String message = object.getString("message"); 
      Log.d("firebasenotification",title+",,"+message); 
      sendNotificatin(title,message); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    }else if(remoteMessage.getNotification()!=null){ 
     Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); 
     Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getTitle()); 
     sendNotificatin(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody()); 

    } 
} 

private void sendNotificatin(String title, String message) { 
    Intent intent = new Intent(this,SplashScreen.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent, PendingIntent.FLAG_ONE_SHOT); 
    Uri defaultsoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.mipmap.app_ic) 
      .setContentTitle(title) 
      .setContentText(message) 
      .setAutoCancel(true) 
      .setDefaults(Notification.DEFAULT_SOUND) 
      .setContentIntent(pendingIntent); 
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    Random random = new Random(); 
    int num = random.nextInt(99999-1000)+1000; 
    notificationManager.notify(num,notificationBuilder.build()); 

} 
} 

/////////////////// ///////////////////////////

<service android:name=".Fcm.MyFirebaseInstanceIDService"> 
     <intent-filter> 
      <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> 
     </intent-filter> 
    </service> 
    <service 
     android:name=".Fcm.MyFirebaseMessagingService"> 
     <intent-filter> 
      <action android:name="com.google.firebase.MESSAGING_EVENT"/> 
     </intent-filter> 
    </service> 

    <meta-data 
     android:name="com.google.firebase.messaging.default_notification_icon" 
     android:resource="@mipmap/app_ic" /> 
+0

あなたが助けを求めたい場合は、あなたのコードを追加してください! –

+0

コードを確認してください –

+0

私は ".setSound(defaultsoundUri)"でテストしました。 –

答えて

0

私のコードを編集しましたが、 .setSoundを追加するのを忘れた。私はまたあなたが将来的に望むかもしれないいくつかの他のものを加えました。

private void sendNotificatin(String title, String message) { 

private void sendNotificatin(String title, String message) { 
    Intent intent = new Intent(this,SplashScreen.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent, PendingIntent.FLAG_ONE_SHOT); 
    Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.mipmap.app_ic) 
      .setContentTitle(title) 
      .setContentText(message) 
      .setAutoCancel(true) 
      .setSound(sound) // changed from SET_DEFAULTS 
      .setContentIntent(pendingIntent); 
      /* Begin other added stuff */ 
      .setLights(0xffff0000, 500, 250); 
      .setWhen(System.currentTimeMillis()) 
      /* End other added stuff */ 
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    Random random = new Random(); 
    int num = random.nextInt(99999-1000)+1000; 
    notificationManager.notify(num,notificationBuilder.build()); 

    } 
+0

私はすべてを行いました –

+0

私のコードを編集しました。 – Destry

1

私はコメントを追加するには十分な評判がないので、私はあなたのためにこれをやっていますが、これは不適切だとわかります。

アプリが最小化されたとき、またはバックグラウンドで、Android通知トレイに配信されたAndroid通知に配信された通知が理由です。 「データペイロード」と「通知ペイロード」の2つのペイロードがあります。ちょうど "通知のペイロード"を削除すると正常に動作します。

関連する問題