如何通过程序将NotificationChannel的重要性设置为紧急状态?

时间:2018-08-03 14:22:36

标签: android

我想实现类似于whatsapp的聊天notification()(Whatsapp不知道如何,但是将组消息频道重要性(Android O)重置为紧急)。 一切都很好,我无法通过编程将NotificationChannel的重要性设置为“紧急”。

这是我的通知书。

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

    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra(Const.Type.KEY, Const.Type.NOTIFICATION);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "alert_001")
            .setSmallIcon(R.drawable.ic_stat_s)
            .setWhen(System.currentTimeMillis())
            .setContentTitle("Booking Request")
            .setContentText(content)
            .setPriority(Notification.PRIORITY_MAX)
            .setContentIntent(pendingIntent)
            .setOngoing(false)
            .setAutoCancel(true);
    if (mNotificationManager != null) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(Const.Channel.Id.ALERT_001,
                    Const.Channel.Name.BOOKING_ALERT,
                    NotificationManager.IMPORTANCE_HIGH);
            /* Here is no constant like NotificationManager.IMPORTANCE_URGENT
             * and i can't even put the another integer value, It not obeying the rules*/

            mNotificationManager.createNotificationChannel(channel);
            mBuilder.setChannelId(channel.getId());
        }
        mNotificationManager.notify(1, mBuilder.build());
    }

虽然我正在更改如下所示通知的JSON响应

{
  priority=high,
  message=msgmsg,
  source_lat=22.751552,
  source_lng=75.895745,
  user_detail={},
}

至少它像 NotificationManager.IMPORTANCE_HIGH 一样工作,但不像 URGENT 。 我不知道whatsapp如何实现可以覆盖 URGENT 的重要性,即使用户手动更改它也是如此。

2 个答案:

答案 0 :(得分:1)

有一个骇客:

private String ensureChannelAudibility(String channelMainId, String channelName, Uri soundUri) {
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(App.instance);
    List<NotificationChannel> channels = notificationManager.getNotificationChannels();
    NotificationChannel existingChanel = null;
    int count = 0;
    for (NotificationChannel channel : channels) {
        String fullId = channel.getId();
        if (fullId.contains(channelMainId)) {
            existingChanel = channel;
            String[] numbers = extractRegexMatches(fullId, "\\d+");
            if (numbers.length > 0) {
                count = Integer.valueOf(numbers[0]);
            }
            break;
        }
    }

    if (existingChanel != null) {
        if (existingChanel.getImportance() < NotificationManager.IMPORTANCE_DEFAULT) {
            notificationManager.deleteNotificationChannel(existingChanel.getId());
            existingChanel = null;
        }
    }

    if (existingChanel == null) {
        String newId = channelMainId+'_'+(count+1);
        NotificationChannel notificationChannel = new NotificationChannel(newId, channelName, NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(notificationChannel);
        return newId;
    } else {
        return existingChanel.getId();
    }
}

//Show the notification with channel id returned by the method above

public String[] extractRegexMatches(String source, String regex) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(source);

        ArrayList<String> matches = new ArrayList<>();
        while (matcher.find()) {
            matches.add(matcher.group());
        }

        String[] result = new String[matches.size()];
        matches.toArray(result);
        return result;
    }

通常,您只能在创建频道时对其进行设置。一旦用户更改了它,您将无法以编程方式将其恢复。上面的代码欺骗了系统。它检测通道重要性是否低于所需的重要性,如果存在,则将其删除并创建另一个具有不同ID的重要性。因此,系统的工作是其他事情需要一个不同的渠道。

因此,假设您有一个频道为“ channelMainId” = wolf。将创建一个(wolf_1)频道。用户更改设置后,下一个频道将为(wolf_2),然后 wolf_3,wolf_4 ... wolf_xxxxxxxxxxxx。

答案 1 :(得分:0)

请尝试以下操作:

        String channelId = "YOUR_CHANNEL_ID";
        String channelName = "YOUR_CHANNEL_NAME";
        int importance = NotificationManager.IMPORTANCE_HIGH;
        final NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(
                                            this,channelId)
                                            .setContentTitle("APP_NAME")
                                            .setSmallIcon(//set any Icon)
                                            .setContentText("TEXT")
                                            .setAutoCancel(true);

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

//      for oreo-notification
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    NotificationChannel mChannel = new NotificationChannel(
                                                channelId, channelName, importance);
                                        notificationManager.createNotificationChannel(mChannel);
                                    }

谢谢