安卓通知声音不断重复的问题

3
以下是我的通知生成代码,但声音会一直重复播放,直到下拉状态栏检查它。
NotificationManager mNotificationManager;
        android.support.v7.app.NotificationCompat.Builder mBuilder;
        Notification mNotification;
        final int NOTIFICATION_ID = 1;

        mNotificationManager = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, new Intent(ctx, HomeScreenTabbed.class), 0);

        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        mBuilder = new android.support.v7.app.NotificationCompat.Builder(ctx);
        mBuilder.setContentTitle(notificationTitle);
        mBuilder.setStyle(new android.support.v4.app.NotificationCompat.BigTextStyle().bigText(notificationBody));
        mBuilder.setContentText(notificationBody);
        mBuilder.setContentIntent(contentIntent);
        mBuilder.setSound(alarmSound);
        mBuilder.setLargeIcon(BitmapFactory.decodeResource(ctx.getResources(), R.drawable.ic_launcher));
        mBuilder.setSmallIcon(R.drawable.ic_pollen_notification);
        mBuilder.setAutoCancel(true);
        mNotification = mBuilder.build();
        // set dismiss on click flags
        mNotification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_SOUND;
        mNotificationManager.notify(NOTIFICATION_ID, mNotification);

1
可能是重复的问题:如何使自定义通知声音只播放一次 - Pararth
3个回答

7

我通过使用mNotification.defaults替换mNotification.flags解决了这个问题。


2
你刚刚防止了我的应用被删除。 - mihirjoshi

1

这将为所有后续通知静音。此外,我必须删除所有其他标志,以使FLAG_ONLY_ALERT_ONCE正常工作。 - Rajat Saxena

0

就这样:

.setOnlyAlertOnce(true)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接