0

我試圖通知鈴聲通知。我嘗試了很多代碼,但它不起作用。
我想發送默認鈴聲,當我發送通知。 通知是由我發送的。 但移動中沒有聲音。當我發送通知時播放默認鈴聲

我的代碼是

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    private static final String TAG = "FCMPlugin"; 

/** 
* Called when message is received. 
* 
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging. 
*/ 
// [START receive_message] 
@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    // TODO(developer): Handle FCM messages here. 
    // If the application is in the foreground handle both data and notification messages here. 
    // Also if you intend on generating your own notifications as a result of a received FCM 
    // message, here is where that should be initiated. See sendNotification method below. 
    Log.d(TAG, "==> MyFirebaseMessagingService onMessageReceived"); 

    if(remoteMessage.getNotification() != null){ 
     Log.d(TAG, "\tNotification Title: " + remoteMessage.getNotification().getTitle()); 
     Log.d(TAG, "\tNotification Message: " + remoteMessage.getNotification().getBody()); 
    } 

    Map<String, Object> data = new HashMap<String, Object>(); 
    data.put("wasTapped", false); 
    for (String key : remoteMessage.getData().keySet()) { 
      Object value = remoteMessage.getData().get(key); 
      Log.d(TAG, "\tKey: " + key + " Value: " + value); 
      data.put(key, value); 
    } 

    Log.d(TAG, "\tNotification Data: " + data.toString()); 
    FCMPlugin.sendPushPayload(data); 
    //sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), remoteMessage.getData()); 
} 
// [END receive_message] 

/** 
* Create and show a simple notification containing the received FCM message. 
* 
* @param messageBody FCM message body received. 
*/ 
private void sendNotification(String title, String messageBody, Map<String, Object> data) { 
    Intent intent = new Intent(this, FCMPluginActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    for (String key : data.keySet()) { 
     intent.putExtra(key, data.get(key).toString()); 
    } 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
      PendingIntent.FLAG_ONE_SHOT); 

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(getApplicationInfo().icon) 
      .setContentTitle(title) 
      .setContentText(messageBody) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

    NotificationManager notificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
} 

}

回答

0

如果你想這個模擬器上進行,因爲,如果我沒有記錯的話,它沒有一個默認鈴聲設置爲它不會工作:

content://settings/system/ringtone 

您是否嘗試過測試

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(getApplicationInfo().icon) 
      .setContentTitle(title) 
      .setContentText(messageBody) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

在實際設備上?

+0

是的,我已經嘗試過。但它不適用於任何移動設備。 –

0

檢查您的應用程序是否在後臺。如果應用程序在後臺,onMessageReceived函數不會被調用,因此沒有鈴聲。