【问题标题】:Android Notification Multiple DefaultsAndroid 通知多个默认值
【发布时间】:2017-01-07 12:06:17
【问题描述】:

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

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 对象,然后设置标志(这似乎在我的设备上与振动、自动取消和警报一起使用,但随后其他用户报告说现在根本没有收到通知)。

因此,我怎样才能使用我当前的代码,同时设置振动、自动取消和警报一次三个默认值?

【问题讨论】:

  • 你试过|操作符来组合默认值吗?
  • @KNeerajLal,谢谢! builder.setDefaults(Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE);这似乎有效。
  • 很高兴您找到了答案。我应该发布答案吗?
  • @KNeerajLal,是的,请,然后我会将其标记为已接受的答案。

标签: java android android-intent android-notifications


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    • 2012-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多