当我的应用程序打开时(前台),我可以获得定制的通知声音,但当它关闭时(后台),我无法获得

yyhrrdl8  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(466)

public void onmessagereceived(远程消息远程消息){

Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message"));
    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);
    String channelId =getString(R.string.channelid);

    Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"+ getApplicationContext().getPackageName() + "/" + R.raw.fancy);
    NotificationManager mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationChannel mChannel;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        mChannel = new NotificationChannel(channelId, "Notification Tone", NotificationManager.IMPORTANCE_HIGH);
        mChannel.setLightColor(Color.GRAY);
        mChannel.setDescription(remoteMessage.getNotification().getBody());
        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .build();
        mChannel.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.fancy), audioAttributes);

        if (mNotificationManager != null) {
            mNotificationManager.createNotificationChannel( mChannel );
        }
    }

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.drawable.logo)
            .setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.fancy))
            .setContentTitle(remoteMessage.getNotification().getTitle())
            .setContentText(remoteMessage.getNotification().getBody())
            .setAutoCancel(true).setContentIntent(pendingIntent)
            .setPriority(NotificationCompat.PRIORITY_HIGH);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
        mBuilder.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.fancy));
    }

    int NOTIFICATION_ID = 1; 
    if (mNotificationManager != null) {
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
}

我正在为我的android应用程序使用fcm通知。当我的应用程序打开时(前台),我可以得到定制的通知声音。但是当它关闭时(后台),我不能得到它。我不明白我哪里错了,我在下面提到了代码

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题