【问题标题】:How to implement Firebase Cloud messaging with Foreground application?如何使用前台应用程序实现 Firebase 云消息传递?
【发布时间】:2016-06-11 05:55:11
【问题描述】:

如何在前台应用程序的Activity中接收消息并使用Toast显示消息?我仅在应用处于后台时才会收到通知。

类似于实时消息。

请帮帮我!!

【问题讨论】:

  • 你应该为它创建一个服务
  • 在我的回答中,只需复制并粘贴服务类并将其包含在其中,它将在后台运行
  • 你知道firebase是一个实时数据库,对吧?只要您的应用操作系统连接到数据库,每次更新(取决于您的实现)都会立即收到。
  • @intj 好主意。但请记住,使用 Firebase 数据库会使(额外的)套接字保持打开状态。如果更新相对频繁,那可能是值得的。但对于其他场景,使用云消息更“环境”的通知机制可能更可取。
  • @FrankvanPuffelen 感谢您提及这一点。我想这一切都取决于他。取决于他的用例。干杯!

标签: android push-notification firebase firebase-cloud-messaging


【解决方案1】:

关于activity的oncreate方法显示toast.write。

registerReceiver(new MyReceiver(),new IntentFilter("MyReceiver"));

然后创建一个 MyReceiver BroadCastReceiver 作为该 Activity 中的内部类。 作为。

public class MyReceiver extends BroadCastReceiver{
    public void onReceive(Context context, Intent intent){
        Toast.makeText(context,intent.getStringExtra("from")+"         "+intent.getStringExtra("message"),Toast.LENGTH_SHORT).show();
    }

}
    ///finally you have to write the following codes on the 
    onMessageReceived
    Intent intents=new Intent();
    intents.setAction("MyReceiver");
    intents.putExtra("message",message.getData().get("message"));
    intents.putExtra("from",message.getData().get("from"));
    getBaseContext().sendBroadcast(intents);

【讨论】:

  • 感谢您坦率地编辑答案,代码排列得不好
【解决方案2】:

Firebase 云消息传递Android Quickstart app 演示了为通知注册 Android 应用并处理消息的接收。 InstanceID 允许轻松注册,而 FirebaseMessagingService 和 FirebaseInstanceIDService 在客户端启用令牌刷新和消息处理。

【讨论】:

  • 但该示例并未演示如何在活动范围内处理 FCM。
猜你喜欢
  • 2023-04-01
  • 2020-01-29
  • 2017-02-25
  • 2017-03-22
  • 2020-12-31
  • 1970-01-01
  • 2021-06-30
  • 2022-12-30
  • 1970-01-01
相关资源
最近更新 更多