![](/img/trans.png)
[英]No notification sound and vibrate and lights when sending notification from firebase in android background
[英]No notification sound when sending notification from firebase in android
我正在將推送通知從 firebase 發送到我的 Android 應用程序。 但是當我的應用程序在后台 firebase onMessageReceived 方法不被調用而是 firebase 向系統發送通知以在系統托盤中顯示通知。 通知出現在系統托盤中,但沒有通知聲音,即使我在系統設置中允許我的應用程序發出通知聲音。
當從 firebase 收到通知時,我可以做些什么來播放通知聲音。
這就是我將通知從firebase發送到我的應用程序Blogpost 鏈接的方式。
在通知的通知負載中有一個聲音鍵。
從官方文檔來看,它的用法是:
表示設備收到通知時播放的聲音。 支持應用程序中捆綁的聲音資源的默認或文件名。 聲音文件必須位於 /res/raw/ 中。
例如:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon",
"sound" : "mySound"
}
}
如果你想使用設備的默認聲音,你應該使用: "sound": "default"
。
有關有效負載中所有可能的密鑰,請參閱此鏈接: https : //firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support
對於那些不知道 firebase 的人來說,當應用程序在后台時,它會以不同的方式處理通知。 在這種情況下,不會調用 onMessageReceived 函數。
當您的應用程序在后台時,Android 會將通知消息定向到系統托盤。 默認情況下,用戶點擊通知會打開應用啟動器。 這包括同時包含通知和數據負載的消息。 在這些情況下,通知會傳送到設備的系統托盤,數據有效負載會傳送到啟動器 Activity 的意圖的附加內容中。
試試這個
{
"to" : "DEVICE-TOKEN",
"notification" : {
"body" : "NOTIFICATION BODY",
"title" : "NOTIFICATION TITILE",
"sound" : "default"
}
}
@note 自定義通知聲音:-> "sound" : "MyCustomeSound.wav"
僅當應用程序處於前台或通知負載僅包含數據類型時才會觸發onMessageReceived
方法。
對於下游消息傳遞,FCM 提供兩種類型的有效負載:通知和數據。
對於通知類型,FCM 會代表客戶端應用程序自動向最終用戶設備顯示消息。 通知具有一組預定義的用戶可見鍵。
對於數據類型,客戶端應用程序負責處理數據消息。 數據消息只有自定義鍵值對。當您希望FCM代表您的客戶端應用程序處理顯示通知時,請使用通知。 當您希望您的應用程序在您的 Android 客戶端應用程序上處理顯示或處理消息時,或者如果您希望在存在直接 FCM 連接時向 iOS 設備發送消息,請使用數據消息。
進一步向下看文檔
接收同時包含通知和數據有效載荷的消息時的應用行為取決於應用是在后台還是在前台——本質上,它在接收時是否處於活動狀態。
在后台時,應用程序在通知托盤中接收通知負載,並且僅在用戶點擊通知時處理數據負載。
在前台時,您的應用程序會收到一個消息對象,其中的兩個有效負載都可用。
如果您使用 Firebase 控制台發送通知,則負載將始終包含通知類型。 您必須使用 Firebase API 發送僅包含通知負載中數據類型的通知。 這樣,您的應用程序始終會在收到新通知時收到通知,並且該應用程序可以處理通知負載。
如果您想使用常規方法在應用程序處於后台時播放通知聲音,則需要將聲音參數添加到通知負載中。
使用 HTTP v1 API 則不同
示例:
{
"message":{
"topic":"news",
"notification":{
"body":"Very good news",
"title":"Good news"
},
"android":{
"notification":{
"body":"Very good news",
"title":"Good news",
"sound":"default"
}
}
}
}
我也遇到了必須發出聲音的通知問題,當應用程序在前台時一切正常,但是當應用程序在后台時,聲音就沒有出來。
通知由服務器通過 FCM 發送,即服務器掛載通知的 JSON 並將其發送給 FCM,然后由 FCM 將通知發送給應用程序。 即使我放了聲音標簽,聲音也不會在背景中出現。
即使把聲音標簽它也不起作用。
經過如此多的搜索,我在 github 論壇上找到了解決方案。 然后我注意到我的情況有兩個問題:
1 - 缺少發送 channel_id 標簽,這對於在 API 級別 26+ 中工作很重要
2 - 在 Android 應用程序中,對於這種直接從服務器發送通知的特定情況,我必須提前配置通道 ID,因此在我的主 Activity 中,我必須配置通道,以便 Android 知道何時該做什么通知到了。
在服務器發送的 JSON 中:
{
"title": string,
"body": string,
"icon": string,
"color": string,
"sound": mysound,
"channel_id": videocall,
//More stuff here ...
}
在您的主要活動中:
@Background
void createChannel(){
Uri sound = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.app_note_call);
NotificationChannel mChannel;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mChannel = new NotificationChannel("videocall", "VIDEO CALL", NotificationManager.IMPORTANCE_HIGH);
mChannel.setLightColor(Color.GRAY);
mChannel.enableLights(true);
mChannel.setDescription("VIDEO CALL");
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
mChannel.setSound(sound, audioAttributes);
NotificationManager notificationManager =
(NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(mChannel);
}
}
這終於解決了我的問題,我希望它可以幫助人們不要像我一樣浪費 2 天。 我不知道我在代碼中輸入的所有內容是否都需要,但就是這樣。 我也沒有找到 github 論壇鏈接來證明答案,因為我所做的與發布在那里的相同。
這樣做
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//codes..,.,,
Uri sound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(sound);
}
試試這個....
public void buildPushNotification(Context ctx, String content, int icon, CharSequence text, boolean silent) {
Intent intent = new Intent(ctx, Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 1410, intent, PendingIntent.FLAG_ONE_SHOT);
Bitmap bm = BitmapFactory.decodeResource(ctx.getResources(), //large drawable);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(ctx)
.setSmallIcon(icon)
.setLargeIcon(bm)
.setContentTitle(content)
.setContentText(text)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
if(!silent)
notificationBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1410, notificationBuilder.build());
}
並在 onMessageReceived 中調用它
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d("Msg", "Message received [" + remoteMessage.getNotification().getBody() + "]");
buildPushNotification(/*your param*/);
}
或者按照KongJing ,他說的也是正確的,但是您可以使用 Firebase 控制台。
即使我從 firebase 控制台發送通知聲音,我也可以播放通知聲音。 要做到這一點,您只需要在預先選項中添加值為“default”的鍵“sound”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.