繁体   English   中英

Android Notificationcompat声音/振动不起作用

[英]Android notificationcompat sound/vibration not working

我花了最后2个小时来弄清楚为什么从FireBase发送的通知没有发出声音或振动的原因。

我已经看过许多有关此问题的主题,并尝试使用.setDefaults .setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 }) .setSound(defaultSoundUri) .setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 }) .setSound(defaultSoundUri)

我现在所拥有的是:

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Firebase Push Notification")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setPriority(Notification.PRIORITY_MAX)
            .setDefaults(-1)
            .setContentIntent(pendingIntent);

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

    notificationManager.notify(0, notificationBuilder.build());

我没有任何错误,并且显示通知,但是我重复一次,没有声音,没有振动,没有led灯,没有抬头。

要发送通知,我在服务器上使用python库:

# Send to single device.
from pyfcm import FCMNotification

push_service = FCMNotification(api_key="xxxxxxxxxxxxxxxx")

registration_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
message_title = "Test"
message_body = "Test notification"
result = push_service.notify_single_device(registration_id=registration_id, message_title=message_title, message_body=message_body)

print result

注意:我已经在清单中定义了权限: <uses-permission android:name="android.permission.VIBRATE" />任何想法?

您没有从服务器发送声音标记,这也许就是为什么您没有收到通知声音的原因。 请尝试添加:

sound = "default"

这可能会使您听到通知的声音。

看看这个:

https://firebase.google.com/docs/cloud-messaging/http-server-ref

为了更好地了解fcm下游android通知标志。

让我知道它是否对您有任何改变。

为了能够使用声音和振动,请检查您是否具有正确的权限:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM