繁体   English   中英

当应用程序处于后台或被杀死时,我想在 Android 中显示自定义 firebase 推送通知 state

[英]I want to show a customize firebase push notification in Android when app is in background or killed state

我想实现自定义通知行为,如 WhatsApp 或任何其他应用程序,用户可以在其中与托盘中的通知进行交互,键入消息并从那里间接发送消息。

所以,到目前为止,当应用程序位于前台时,我已经成功地显示了带有 2 个按钮的自定义通知。 当我在 firebase 服务的 onMessageReceived() function 中获得控制时,我通过简单地显示自定义通知来实现这一点。

根据我的研究和工作,当应用程序在后台或杀死 state 时,您无法控制服务。

那么,问题是您如何实现这一目标? 我想显示带有标题、正文和 2 个按钮的通知。 单击按钮后,我想执行 2 个不同的任务,即通过打开应用程序调用 API。

任何帮助,将不胜感激。

发送推送通知时有两种类型的有效负载。

  1. 通知和数据有效负载:如果应用程序处于终止/后台 state,则通知有效负载将由系统托盘处理以显示通知,并且在单击通知时,将在启动器活动的意图中接收数据有效负载。(getIntent().getExtras( ))

  2. 数据有效载荷:如果仅包含“数据”有效载荷,则将为每个 state 前景/背景/被杀死的 state 调用 onMessageReceived()。因此您可以创建通知,点击处理也将从此处提供挂起的意图。

看看 Firebase SDK 的实现。

 private void dispatchMessage(Intent intent) { Bundle data = intent.getExtras(); if (data == null) { // The intent should always have at least one extra so this shouldn't be null, but // this is the easiest way to handle the case where it does happen. data = new Bundle(); } // First remove any parameters that shouldn't be passed to the app // * The wakelock ID set by the WakefulBroadcastReceiver data.remove("androidx.content.wakelockid"); if (NotificationParams.isNotification(data)) { NotificationParams params = new NotificationParams(data); ExecutorService executor = FcmExecutors.newNetworkIOExecutor(); DisplayNotification displayNotification = new DisplayNotification(this, params, executor); try { if (displayNotification.handleNotification()) { // Notification was shown or it was a fake notification, finish return; } } finally { // Ensures any executor threads are cleaned up executor.shutdown(); } // App is in the foreground, log and pass through to onMessageReceived below if (MessagingAnalytics.shouldUploadScionMetrics(intent)) { MessagingAnalytics.logNotificationForeground(intent); } } onMessageReceived(new RemoteMessage(data)); }

 /** * Handle a notification message by displaying it if appropriate, and returning whether the * message is consumed. * <li>If this is a no UI notification just used for analytics, doesn't show a notification and * returns true. * <li>If the app is in the foreground, doesn't show a notification, and returns false. * <li>If the app is in the background, shows a notification and returns true. */ boolean handleNotification() { if (params.getBoolean(MessageNotificationKeys.NO_UI)) { return true; // Fake notification, nothing else to do } if (isAppForeground()) { return false; // Needs to be passed to onMessageReceived } ImageDownload imageDownload = startImageDownloadInBackground(); CommonNotificationBuilder.DisplayNotificationInfo notificationInfo = CommonNotificationBuilder.createNotificationInfo(context, params); waitForAndApplyImageDownload(notificationInfo.notificationBuilder, imageDownload); showNotification(notificationInfo); return true; }

 public static boolean isNotification(Bundle data) { return "1".equals(data.getString(MessageNotificationKeys.ENABLE_NOTIFICATION)) || "1".equals(data.getString(keyWithOldPrefix(MessageNotificationKeys.ENABLE_NOTIFICATION))); }

从实现中,您可以看到应用程序仅在以下情况下具有控制权:

  • 该消息不是推送通知(这是数据消息)
  • 该应用程序在前台运行。

否则,Firebase SDK 将接管并操纵通知。 这是 Firebase 的一个奇怪设计,因为当应用程序在后台和前台时,它可能会导致通知不一致。 Firebase 在这里也提到了但不是很明显。

然后,解决方案:

  • 要求后端将消息更改为data message而不是notification message
  • 或者,通过 firebase sdk 的条件添加 hack,这使得NotificationParams.isNotification(data)返回false

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM