gpt4 book ai didi

java - Android Notification Not getting Sound- FCM 使用 PHP

转载 作者:行者123 更新时间:2023-11-29 02:30:30 24 4
gpt4 key购买 nike

我在我的 android 应用程序中使用 php 发送推送通知。它工作正常,但在某些设备中,我在通知期间听不到声音。通知来了显示,但它不播放声音。我已经测试了三台三星设备(6.0、7.0)并且都有同样的问题。在我的 OnePlus 设备中出现声音。 我的 PHP 代码如下所示

<?php
function sendFCM($notification, $data, $token)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$server_key = 'MyKEY';

$fields = array();
$fields['notification'] = $notification;
$fields['data'] = $data;
$fields['to'] = $token;

$headers = array
(
'Content-Type:application/json',
'Authorization:key=' . $server_key
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE)
die('FCM Send Error: ' . curl_error($ch));

curl_close($ch);
return $result;
}

sendFCM(array("title" => $row[0] . " is Online", "body" => "",'priority'=>'high'), array("message" => ""), $row[1]);

我的安卓代码如下

public class FCMMessageHandler extends FirebaseMessagingService
{
public static final int MESSAGE_NOTIFICATION_ID = 6545;

@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
RemoteMessage.Notification notification = remoteMessage.getNotification();
assert notification != null;
createNotification(notification);
}

private void createNotification(RemoteMessage.Notification notification)
{
Context context = getBaseContext();

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher).setContentTitle(notification.getTitle())
.setContentText(notification.getBody());
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
if (mNotificationManager != null) {
mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build());
}
}
}

如果我遗漏了什么,请告诉我。谢谢

最佳答案

尝试在 createNotification 中使用 ToneGenerator

ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, ToneGenerator.MAX_VOLUME);
toneG.startTone(ToneGenerator.TONE_CDMA_HIGH_L, 3000);

你也可以添加振动

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

((Vibrator)getSystemService(VIBRATOR_SERVICE)).vibrate(2000);

关于java - Android Notification Not getting Sound- FCM 使用 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49788840/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com