Android通知使用PHP无法获得Sound-FCM

时间:2018-04-12 05:45:21

标签: java android firebase firebase-cloud-messaging

我在我的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]);

我的Android代码如下所示

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());
        }
    }
}

如果我错过了什么,请告诉我。 感谢

3 个答案:

答案 0 :(得分:1)

尝试在ToneGenerator

中使用createNotification
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);

答案 1 :(得分:0)

从firebase发送推送通知时,请确保启用声音选项 还要确保你在android清单中添加了振动权限。

答案 2 :(得分:-1)

Plz包含以下代码: -

TO_DATE(DAY_DESC,'MM/DD/YYYY') BETWEEN DATE '{}' AND DATE '{}'

一点点文档here

以前它是使用setDefaults()处理的,但此方法在API级别26中已弃用。因此,您必须使用Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); builder.setSound(alarmSound); enableVibration(boolean)以及enableLights(boolean)。 有关详细信息,请查看此link