Android 提示通知未按预期工作

Android 提示通知未按预期工作,android,push-notification,heads-up-notifications,Android,Push Notification,Heads Up Notifications,我已经为我的应用程序实现了提示通知。代码如下: private String CHANNEL_ID = "message_notifications"; NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID,"message_notifications", NotificationManager.IMPORTANCE_HIGH); @Override public void onMessageReceived(R

我已经为我的应用程序实现了提示通知。代码如下:

private  String CHANNEL_ID = "message_notifications";
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID,"message_notifications", NotificationManager.IMPORTANCE_HIGH);


@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);


    String notification_title = remoteMessage.getNotification().getTitle();
    String notification_message = remoteMessage.getNotification().getBody();

    String click_action = remoteMessage.getNotification().getClickAction();


    String user_id = remoteMessage.getData().get("user_id");
    String user_name = remoteMessage.getData().get("user_name");
    String GROUP_KEY_CHIT_CHAT = "com.android.example.Chit_Chat";


    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setSmallIcon(R.drawable.chitchat_icon)
                    .setContentTitle(notification_title)
                    .setAutoCancel(true)
                    .setPriority(NotificationCompat.PRIORITY_MAX)
                    .setGroup(GROUP_KEY_CHIT_CHAT)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
                    .setContentText(notification_message);


    if (Build.VERSION.SDK_INT >= 21) mBuilder.setVibrate(new long[0]);
    Intent resultIntent = new Intent(click_action);
    resultIntent.putExtra("user_id", user_id);
    resultIntent.putExtra("user_name",user_name);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addNextIntentWithParentStack(resultIntent);
    stackBuilder.addParentStack(MainActivity.class);

    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );

    mBuilder.setContentIntent(resultPendingIntent);


    int mNotificationId = (int) System.currentTimeMillis();

    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        mChannel.setShowBadge(true);
        mChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        mNotificationManager.createNotificationChannel(mChannel);
        mBuilder.setChannelId(CHANNEL_ID);
    }

    mNotificationManager.notify(mNotificationId,mBuilder.build());
}
问题是,只有当我将通知设置为弹出和声音时,它才能正常工作,否则它看起来像是简单的通知。Whatsapp和其他应用程序默认设置为使用它们。我也想这样做。我想通过编程将我的应用程序的设置设置为默认情况下使用提醒通知,而无需进入设置以启用它。有人能帮我怎么做吗?

我需要将其设置为“声音”和“弹出”,它在默认情况下不会设置通知频道的

,以显示为提示通知

如果只希望在执行操作时解除通知,请使用方法将通知设置为“正在进行”。用户无法取消正在进行的通知,因此您的应用程序或服务必须负责取消这些通知

如果您还想在请勿打扰模式下显示通知,可以将类别设置为

如果您想要启动意图,而不是将通知发布到状态栏以要求用户立即注意,请使用

示例代码块

        Intent launch = new Intent(context, TargetActivity.class);
        launch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0,
                launch, PendingIntent.FLAG_UPDATE_CURRENT);
        createNotificationChannel(mContext, NOTIFICATION_CHANNEL_ID, NotificationManager.IMPORTANCE_HIGH,
                R.string.notification_channel_name, R.string.notification_channel_description);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID);
        builder.setContentTitle("Title");
        builder.setContentText("Content Text");
        builder.setStyle(new NotificationCompat.BigTextStyle()
                .bigText("Big Content Text"));
        builder.setSmallIcon(R.drawable.status_icon);
        builder.setFullScreenIntent(pendingIntent, true);
        builder.setOngoing(true);
        builder.setAutoCancel(true);
        builder.setCategory(NotificationCompat.CATEGORY_ALARM);
        builder.addAction(0, "Action Text", pendingIntent);
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mContext);
        notificationManager.notify(COMPLETE_NOTIFICATION_ID, builder.build());

   /**
     * create Notification channel
     * @param context
     * @param channelId
     * @param channelName
     * @param channelDescription
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static void createNotificationChannel(Context context, String channelId, int importance, int channelName, int channelDescription) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = context.getString(channelName);
            String description = context.getString(channelDescription);
            NotificationChannel channel = new NotificationChannel(channelId, name, importance);
            channel.setDescription(description);
            NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }

    }

如果您看到我的代码,可能会重复我所做的所有这些解决方案。这是工作,但我需要做一些设置在我的手机上,然后它的工作上述解决方案都在上述代码实现。请先阅读问题,然后再将其标记为重复项。请准确回答。我只是想知道情况。我还设置了高的重要性,但它只有当我长按通知,改变其模式为声音和弹出窗口时才会弹出。我可以通过解除我的应用程序的UALT设置该模式吗?请在我发布时检查是否先创建频道(请参见代码顺序)。然后重新安装(卸载然后重新安装)应用程序以重新创建频道。如果查询不起作用,请使用设备环境更新查询。我认为默认情况下,三星one UI自定义ROM禁用了提示通知,您需要在通知设置中启用它们,如所附屏幕截图中所示。试着用一台运行android/android one的设备。我的问题是,如果SamsungUI不允许升级,那么其他应用程序是如何实现的?我已经上传了我的完整代码。如果序列中有任何错误,请相应地指导我。