Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android studio android 10中未播放通知声音_Android Studio_Firebase Cloud Messaging_Android Notifications - Fatal编程技术网

Android studio android 10中未播放通知声音

Android studio android 10中未播放通知声音,android-studio,firebase-cloud-messaging,android-notifications,Android Studio,Firebase Cloud Messaging,Android Notifications,我通过Firebase云消息发送通知,但声音和振动没有在我的手机上播放,我的手机有Android 10,但声音在Android模拟器上播放,它有Android 9。由于它是一个模拟器,我不能说它有没有振动 我已经检查了其他StackOverflow帖子,但无法获得解决方案 这是我的密码- import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.Pendin

我通过Firebase云消息发送通知,但声音和振动没有在我的手机上播放,我的手机有Android 10,但声音在Android模拟器上播放,它有Android 9。由于它是一个模拟器,我不能说它有没有振动

我已经检查了其他StackOverflow帖子,但无法获得解决方案

这是我的密码-

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

/**
 * NOTE: There can only be one service in each app that receives FCM messages. If multiple
 * are declared in the Manifest then the first one will be chosen.
 * <p>
 * In order to make this Java sample functional, you must remove the following from the Kotlin messaging
 * service in the AndroidManifest.xml:
 * <p>
 * <intent-filter>
 * <action android:name="com.google.firebase.MESSAGING_EVENT" />
 * </intent-filter>
 */
public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";

    /**
     * Called when message is received.
     *
     * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
     */
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {


        Log.d(TAG, "From: " + remoteMessage.getFrom());

        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());


        }

        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }


    }


    private void handleNow() {
        Log.d(TAG, "Short lived task is done.");
    }


    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        String channelId = getString(R.string.default_notification_channel_id);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.logo)
                        .setContentTitle("Brain Filter")
                        .setColor(getResources().getColor(R.color.colorAccent))
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

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

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}
导入android.app.NotificationChannel;
导入android.app.NotificationManager;
导入android.app.pendingent;
导入android.content.Context;
导入android.content.Intent;
导入android.media.ringtonemager;
导入android.net.Uri;
导入android.os.Build;
导入android.util.Log;
导入androidx.core.app.NotificationCompat;
导入com.google.firebase.messaging.FirebaseMessagingService;
导入com.google.firebase.messaging.RemoteMessage;
/**
*注意:每个应用程序中只能有一个接收FCM消息的服务。如果多个
*在清单中声明,然后选择第一个。
*
*为了使这个Java示例能够正常工作,必须从Kotlin消息传递中删除以下内容
*AndroidManifest.xml中的服务:
*
* 
* 
* 
*/
公共类MyFirebaseMessagingService扩展了FirebaseMessagingService{
私有静态最终字符串TAG=“MyFirebaseMsgService”;
/**
*收到消息时调用。
*
*@param remoteMessage对象,表示从Firebase云消息传递接收的消息。
*/
@凌驾
收到消息时公共无效(RemoteMessage RemoteMessage){
Log.d(标记“From:”+remoteMessage.getFrom());
如果(remoteMessage.getData().size()>0){
Log.d(标记,“消息数据有效负载:”+remoteMessage.getData());
}
if(remoteMessage.getNotification()!=null){
Log.d(标记,“消息通知正文:”+remoteMessage.getNotification().getBody());
}
}
私有无效handleNow(){
Log.d(标记“短期任务完成”);
}
私有void sendNotification(字符串messageBody){
意向意向=新意向(此,MainActivity.class);
intent.addFlags(intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
PendingEvent PendingEvent=PendingEvent.getActivity(此,0/*请求代码*/,意图,
悬挂式帐篷(一杆旗帜);
String channelId=getString(R.String.default\u notification\u channel\u id);
Uri defaultSoundUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_通知);
NotificationCompat.Builder notificationBuilder=
新建NotificationCompat.Builder(此,channelId)
.setSmallIcon(右可绘制徽标)
.setContentTitle(“大脑过滤器”)
.setColor(getResources().getColor(R.color.colorAccent))
.setContentText(messageBody)
.setAutoCancel(真)
.setSound(defaultSoundUri)
.setContentIntent(挂起内容);
通知经理通知经理=
(NotificationManager)getSystemService(上下文通知服务);
//因为需要android Oreo通知通道。
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.O){
NotificationChannel=新的NotificationChannel(信道ID,
“频道可读标题”,
NotificationManager.重要性(默认值);
notificationManager.createNotificationChannel(频道);
}
notificationManager.notify(通知的0/*ID*/,notificationBuilder.build());
}
}

我还在manifest.xml中添加了振动和互联网权限,并在推送任何通知时始终在Firebase控制台中启用声音。

卸载应用程序并重新安装将解决此问题


这些设置是在您第一次创建频道时设置的,然后不进行修改,除非您通过重新安装或清除数据手动进行设置。

您是否检查了您测试的设备的声音和振动设置是,而不是我在android 6、7、8.1、9和10上测试了这些设置。这个问题只发生在android 10中。我仍然在寻找解决方案,但没有找到任何东西。一些我甚至无法想象但却能完美工作的东西。我还有两个问题1。如果有人向我发送消息,则会出现一个通知,当同一用户向我发送另一条消息时,以前的通知将被新的通知替换,我不知道如何保留来自同一用户的所有消息,最多保留5条消息,或者如何显示来自该用户的通知计数。第二个问题是,当我们与某个用户聊天时,如何自动删除该用户的通知。这两个功能都可以参考WhatsApp