【问题标题】:How to listen out for firebase notifications on any activity如何收听任何活动的 Firebase 通知
【发布时间】:2017-02-01 22:14:38
【问题描述】:

我已经按照几个教程将通知从 firebase 发送到 Android 应用程序,并且我能够毫无问题地接收通知。

我想了解两件事:

  1. 当用户查看不处理通知的活动时,如何处理接收通知?

  2. 当应用程序正在运行但不在前台并且我收到通知时,当我从系统托盘单击它时,应用程序会打开定义了广播者的活动 - 我如何控制哪个活动是打开了吗?

这是处理通知的活动的代码

public class DisplayNotificationActivity extends AppCompatActivity {
    private BroadcastReceiver mRegistrationBroadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_notification);

        mRegistrationBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // checking for type intent filter
            if (intent.getAction().equals(Config.REGISTRATION_COMPLETE)) {
                // gcm successfully registered
                // now subscribe to `global` topic to receive app wide notifications
                FirebaseMessaging.getInstance().subscribeToTopic(Config.TOPIC_GLOBAL);
            } else if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) {
                // new push notification is received
                String message = intent.getStringExtra("message");
                Toast.makeText(getApplicationContext(), "Push notification: " + message, Toast.LENGTH_LONG).show();
                txtMessage.setText(message);
            }
        }
    };

如果您需要查看代码的其他部分,请告诉我。

【问题讨论】:

    标签: android notifications


    【解决方案1】:

    当用户查看不处理通知的活动时,如何处理接收通知?

    我会尽量缩短。当您的应用程序处于前台时,通知将在您的FirebaseMessagingService 实现的onMessageReceived()method 中收到。现在您应该将事件广播到哪个活动?创建一个 Base Activity 类并让所有的 Activity 扩展它。在 BaseActivity 中,您可以使用广播方法或尝试使用一些 Eventbus 库,就像我这样:

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void onNotificationReceived(NotificationReceieveBean bean){
    
     if(bean != null){
    
           ViewUtils.showJobOfferSnackBar(this, bean.map);
    
           EventBus.getDefault().removeStickyEvent(bean);
        }
    
    
    
    }
    

    任何扩展 Base Activity 的 Activity 都将能够接收到通知事件。你可以相应地处理它。

    当应用程序正在运行但不在前台并且我收到通知时,当我从系统托盘单击它时,应用程序会打开定义广播器的活动 - 我如何控制打开哪个活动?

    通常当您单击系统生成的通知时,您的第一个活动会打开。您必须检查 Bundle 实例中的数据是否与您从服务器发送的数据具有相同的键,在 onCreate() 示例中:

        Bundle bundle = getIntent().getExtras();
    
        if(bundle != null){
            // if the below condition is true then it means you have received notification
            if(bundle.getString("notification_key_sent_from_server") != null) {
                ViewUtils.handleIncomingFCMNotification(bundle);
    
                finish(); // finishing the first activity since I want to go to some other activity
                return;
    
            }
    
        }
    

    ** 编辑 **

    public abstract class BaseNotificationHandlerActivity extends AppCompatActivity {
    
     protected int activityFlag = 0; // set a unique value from each extending Activity
     @Override
     public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
    }
    
     @Override
     public void onStart(){
      super.onStart();
      EventBus.getDefault().register(this);
    }
    
      @Override
      public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
     }
    
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(NotificationItem notif) {
      if(activityFlag == 1){ 
       // this means Starting Activity, so handle for it particularly
           NotificationHelper notifHelper = new NotificationHelper(this);
           notifHelper.AddNotificationItem(notif);
       }
      else if(activityFlag ==2){
        // for some other Activity
        }
    
      }
    }
    
    
    public class StartingActivity extends BaseNotificationHandlerActivity {
    
     @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_starting);
      activityFlag = 1; // add unique flag for Activity here
    }
    
     @Override
     public void onResume() {
     super.onResume();
    }
    

    【讨论】:

    • 好的 - 关于问题 1,对于某些活动,我如何以不同的方式处理通知的接收?
    • @andrewb,只需在 BaseActivity 中创建一个处理通知的方法,如果实施活动想要以不同方式处理通知,则应覆盖该方法:)
    • 你提到 BaseActivit,y,在我的例子中是 DisplayNotificationActivity 吗?
    • 在我的回答中,我已经解释了 BaseActivity 的用途和用法。你所有的活动都应该扩展它。你可以随意命名。
    • 我正在尝试扩展它,但是当我这样做时,广播监听器没有收到通知
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 2011-06-05
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    相关资源
    最近更新 更多