通知提示未按预期工作

4

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

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和其他应用程序默认设置为使用它们。我想做同样的事情。我想通过编程方式设置我的应用程序的设置,以默认使用悬浮通知,而不必进入设置来启用它。有人可以帮助我如何做到这一点吗? 在此输入图像描述 我需要将其设置为声音和弹出窗口,它不会默认设置。

https://dev59.com/QFwX5IYBdhLWcg3wrRI5 - Kiran
可能是如何在Android上显示悬浮通知的重复问题。 - Edric
如果您查看我的代码,会发现我已经尝试了所有这些解决方案。它可以正常工作,但我需要在手机上进行一些设置才能使其正常运行。 - Rehan
以上解决方案均在上述代码中实现。请在将其标记为重复之前先阅读问题。 - Rehan
1个回答

4

将通知渠道的重要性级别设置为IMPORTANCE_HIGH以显示为悬浮通知。

如果您希望通知在执行操作时才被解除,请使用setOngoing方法将通知设置为持续。持续通知无法被用户解除,因此您的应用程序或服务必须负责取消它们。

如果您希望在“请勿打扰”模式下显示通知,也可以将类别设置为CATEGORY_ALARM

如果您希望启动意图而不是将通知发布到状态栏以引起用户的立即注意,请使用setFullScreenIntent

示例代码块

        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);
        }

    }

1
请先检查我发布的创建频道的代码(请注意代码顺序)。然后进行全新安装(卸载再重新安装)应用程序以重新创建频道。 - Anoop M Maddasseri
我已经上传了完整的代码。如果序列中有任何错误,请相应地指导我。 - Rehan
@rehan,你找到任何解决方案了吗? - Rahul
@Rahul,不好意思,我做不到。 - Rehan
您的通道需要显式设置setSound(uri)和setVibrate(pattern),这就是为什么悬浮通知无法正常工作的原因。在Oreo之前,我们可以在Notification.Builder对象中修改这些变量,但现在我们必须在Notification Channel中进行设置。 - zuko
显示剩余5条评论

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