2017-03-11 123 views
0

我正在使用fcm在我的android應用程序中獲取推送通知。我收到的通知很好,當應用程序打開和關閉或最小化狀態also.But問題是我沒有得到通知聲音時,應用程序在最小化或關閉狀態。當應用程序關閉或最小化時推送通知不會響起

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通知。有兩個有效載荷,一個是「數據有效載荷」,另一個是「通知有效載荷」。只要刪除「通知有效載荷」,那麼它會正常工作。

相關問題