如何只播放一次自定义通知声音

时间:2014-09-12 05:29:13

标签: android notifications

我已经创建了包含自定义声音,振动和一些短信的通知。但是,我无法通知只播放一次自定义声音。 在我的情况下,它继续播放,直到用户拉出抽屉 这是我的代码,

 public synchronized static void createNotification(Context context,
            String message, int soundFile, int notifyId) {
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                context);
        Intent notificationIntent = new Intent(context, DashBoardActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent contentIntent;
        contentIntent = PendingIntent.getActivity(context, 0,
                notificationIntent, 0);
        mBuilder.setSmallIcon(R.drawable.notification)
                .setContentIntent(contentIntent)
                .setContentTitle(
                        context.getResources().getString(
                                R.string.schoolezone_map_title))
                .setContentText(message)
                .setAutoCancel(true)
                .setStyle(
                        new NotificationCompat.BigTextStyle().bigText(message));

        Uri sound = Uri.parse("android.resource://" + context.getPackageName()
                + "/" + soundFile);
        mBuilder.setSound(sound, 1);
        int mNotificationId = notifyId;
        NotificationManager mNotifyMgr = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = mBuilder.build();
        // notification.sound = sound;
        notification.flags |= Notification.DEFAULT_LIGHTS;
        notification.flags |= Notification.DEFAULT_VIBRATE;
        mNotifyMgr.notify(mNotificationId, notification);
    }

请告诉我一些解决方案,有用的帮助将非常感谢 谢谢!

1 个答案:

答案 0 :(得分:4)

确定添加此标志

notification.flags = Notification.FLAG_ONLY_ALERT_ONCE;

来自Google文档Googlde Docs

这意味着

FLAG_ONLY_ALERT_ONCE:如果您希望每次发送通知时都要播放声音和/或振动,即使在此之前尚未取消,也应设置。