Android的Firebase推送通知在几秒钟后消失,如何在显示之前保持通知直到采取任何操作?
创始人
2024-10-07 05:34:36
0

要保持Firebase推送通知在显示之前保持直到采取任何操作,您可以使用自定义布局和自定义通知操作来实现。

首先,创建一个自定义布局文件custom_notification_layout.xml,用于定义通知的外观。例如,以下是一个简单的布局示例:



    

    


然后,在接收到Firebase推送通知时,使用自定义布局创建通知,并添加自定义操作。以下是一个使用通知管理器创建自定义通知的示例代码:

// 使用自定义布局创建通知
RemoteViews customNotificationView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
customNotificationView.setTextViewText(R.id.notification_title, "标题");
customNotificationView.setTextViewText(R.id.notification_message, "消息内容");

// 创建自定义操作
Intent actionIntent = new Intent(this, CustomNotificationActionReceiver.class);
actionIntent.setAction("ACTION_CUSTOM_ACTION");
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(this, 0, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT);

// 添加自定义操作到通知
customNotificationView.setOnClickPendingIntent(R.id.custom_action_button, actionPendingIntent);

// 创建通知构建器
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setCustomContentView(customNotificationView)
        .setAutoCancel(true);

// 显示通知
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, builder.build());

在上面的代码中,我们首先使用RemoteViews创建了一个自定义布局,然后使用setTextViewText方法设置布局中的文本内容。接下来,我们创建了一个自定义操作,该操作将在用户点击通知中的按钮时触发。然后,我们使用setOnClickPendingIntent方法将自定义操作添加到自定义布局中的按钮。最后,我们使用NotificationCompat.Builder创建了一个通知构建器,并使用setCustomContentView方法将自定义布局添加到通知中。

请注意,上述代码中的CustomNotificationActionReceiver是一个自定义广播接收器,用于处理自定义操作的点击事件。您需要根据自己的需求实现该广播接收器。

通过以上步骤,您可以在显示Firebase推送通知之前保持通知直到采取任何操作。

相关内容

热门资讯

Android Recycle... 要在Android RecyclerView中实现滑动卡片效果,可以按照以下步骤进行操作:首先,在项...
安装apache-beam==... 出现此错误可能是因为用户的Python版本太低,而apache-beam==2.34.0需要更高的P...
Android - 无法确定任... 这个错误通常发生在Android项目中,表示编译Debug版本的Java代码时出现了依赖关系问题。下...
Android - NDK 预... 在Android NDK的构建过程中,LOCAL_SRC_FILES只能包含一个项目。如果需要在ND...
Akka生成Actor问题 在Akka框架中,可以使用ActorSystem对象生成Actor。但是,当我们在Actor类中尝试...
Agora-RTC-React... 出现这个错误原因是因为在 React 组件中使用,import AgoraRTC from “ago...
Alertmanager在pr... 首先,在Prometheus配置文件中,确保Alertmanager URL已正确配置。例如:ale...
Aksnginxdomainb... 在AKS集群中,可以使用Nginx代理服务器实现根据域名进行路由。以下是具体步骤:部署Nginx i...
AddSingleton在.N... 在C#中创建Singleton对象通常是通过私有构造函数和静态属性来实现,例如:public cla...
Alertmanager中的基... Alertmanager中可以使用repeat_interval选项指定在一个告警重复发送前必须等待...