gpt4 book ai didi

android - FCM 不工作推送通知声音 android

转载 作者:搜寻专家 更新时间:2023-11-01 09:42:08 24 4
gpt4 key购买 nike

我正在尝试使用 Firebase 控制台向我的手机发送推送通知,但是当应用程序最小化时,通知的声音和标题不起作用,当应用程序在前台工作时正常,行为通知不同的原因是什么?

enter image description here

MyFirebaseMessagingService.java

private void sendNotification(String messageBody) {
Intent intent = new Intent(this, DashboardActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
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(R.drawable.ic_info_black_24dp)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

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

notificationBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });

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

AndroidManifest.xml

    <!-- [START firebase_service] -->
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<!-- [END firebase_service] -->
<uses-permission android:name="android.permission.INTERNET" />

<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

最佳答案

我认为您使用的通知消息类型仅在应用程序位于前台时显示通知。当应用程序处于后台时,您需要使用数据消息类型来获取通知(以及声音、振动等)。查看我的回答here .

关于android - FCM 不工作推送通知声音 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39443131/

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