Android通知多个默认值

时间:2017-01-07 12:06:18

标签: java android android-intent android-notifications

以下是我用来向用户发送通知的代码:

NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.ic_notification)
                    .setContentTitle("Secure Mail")
                    .setContentText("New Secure Mail Received")
                    .setAutoCancel(true)
                    .setOnlyAlertOnce(true);
                    /*
                    .addAction(R.drawable.ic_reply_white_24dp, "Reply", contentIntent)
                    .addAction(R.drawable.ic_reply_all_white_24dp, "Reply All", contentIntent)
                    .addAction(R.drawable.ic_forward_white_24dp, "Forward", contentIntent);
                    */

            builder.setContentIntent(contentIntent);
            builder.setAutoCancel(true);
            builder.setOnlyAlertOnce(true);

            if (ringtone.equals("")) {
                Log.d(TAG, "Default Sound");
                Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                builder.setSound(soundUri);
            } else if (!ringtone.equals("Silent")) {
                Log.d(TAG, "Selected sound: " + Uri.parse(ringtone));
                //Uri currentRintoneUri = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE);
                Uri currentRintoneUri = Uri.parse(ringtone);
                builder.setSound(currentRintoneUri);
            }
            //builder.setLights(Color.BLUE, 500, 500);

            if (vibrate.equals("true")) {
                Log.d(TAG, "Vibrate is enabled");
                //long[] pattern = {500, 500, 500, 500, 500, 500, 500, 500, 500};
                //builder.setVibrate(pattern);

                builder.setDefaults(Notification.DEFAULT_VIBRATE);
            }

            builder.setStyle(new NotificationCompat.InboxStyle());

            /*
            Notification notification = builder.build();
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
            */

            builder.setDefaults(Notification.FLAG_AUTO_CANCEL);
            builder.setDefaults(Notification.FLAG_ONLY_ALERT_ONCE);

            NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
            manager.notify(1, builder.build());

我需要能够有多个默认值(振动,自动取消和提醒一次),但似乎只有最后一次(提醒一次)才有效,而其他人则不会。

我尝试使用Notification对象,然后设置标志(这似乎与我的设备上的振动,自动取消和警报一起工作,但随后其他用户报告现在通知根本没有通过)。

因此,如何使用我当前的代码,还可以设置振动,自动取消和警报一次的三个默认值?

1 个答案:

答案 0 :(得分:2)

您可以使用|运算符组合默认值。

builder.setDefaults(Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE);