奥利奥仅在FCM中播放默认通知

时间:2018-08-09 10:32:05

标签: android push-notification firebase-cloud-messaging

当收到通知时棉花糖和牛轧糖一切都很好,但是当我在oreo通知中测试我的应用程序时,却在手机中选择了播放默认声音。我正在测试2-3奥利奥手机,但结果相同。这是我的通知代码

   private void showNotificationMessage(Context context, String title, String message, Intent intent) {

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

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

    String channelId = "channel-01";
    String channelName = "Channel Name";

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel mChannel = new NotificationChannel(
                channelId, channelName, NotificationManager.IMPORTANCE_HIGH);

        assert notificationManager != null;
        notificationManager.createNotificationChannel(mChannel);
    }

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);


    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
            .setSmallIcon(R.mipmap.vb_grey)
            .setColor(Color.RED)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(Uri.parse("android.resource://"+context.getPackageName()+"/"+R.raw.offic))
            // .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
            .setContentIntent(pendingIntent);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addNextIntent(intent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
            0,
            PendingIntent.FLAG_UPDATE_CURRENT
    );
    mBuilder.setContentIntent(resultPendingIntent);

    notificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());


}

谁能告诉我我在哪里做错了。

1 个答案:

答案 0 :(得分:2)

Notification Builder上的Oreo Notification中,声音设置将不起作用。您需要创建一个NotificationChannel,并需要在NotificationManager上进行设置,以使其按以下方式工作

Uri sound = Uri.parse("android.resource://"+context.getPackageName()+"/"+R.raw.offic);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

    NotificationChannel mChannel = new NotificationChannel("YOUR_CHANNEL_ID",
        "YOUR CHANNEL NAME",
        NotificationManager.IMPORTANCE_DEFAULT)

    AudioAttributes attributes = new AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_NOTIFICATION)
            .build();

    NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, 
            context.getString(R.string.app_name),
            NotificationManager.IMPORTANCE_HIGH);


    mChannel.setDescription(msg);
    mChannel.enableLights(true);
    mChannel.enableVibration(true);
    mChannel.setSound(sound, attributes); // Here you set the sound


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

如果已经使用了通知频道,则该频道将首次无法使用。您需要创建其他频道并为其设置声音以使其正常工作。您可以使用以下代码来检查和删除创建的多个渠道。

if (mNotificationManager != null) {
        List<NotificationChannel> channelList = mNotificationManager.getNotificationChannels();

        for (int i = 0; channelList != null && i < channelList.size(); i++) {
            mNotificationManager.deleteNotificationChannel(channelList.get(i).getId());
        }
    }