【问题标题】:Notify List Adapter when Firebase Notification is received收到 Firebase 通知时通知列表适配器
【发布时间】:2016-11-02 12:40:24
【问题描述】:

我正在尝试在收到通知时向列表视图添加一个新元素(无需用户点击)。

我需要在我的片段上收听收到的通知,然后根据通知信息向适配器添加一个新元素。

如何收听来自片段的新通知?

我已经实现了 Firebase 云消息传递架构。

-MyFirebaseMessagingService......

有人可以帮我吗?

【问题讨论】:

    标签: android android-fragments firebase-cloud-messaging


    【解决方案1】:
     public String push_receiver = "com.example.fragment.GCMBroadCast";
        @Override
        public void onResume() {
            super.onResume();
            mActivity.registerReceiver(broadcastReceiver, new IntentFilter(push_receiver_expositor));
        }
    
        /** receiving GCM broadcast */
        private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
    
                }
            };
    

    【讨论】:

      【解决方案2】:

      在片段中将 Arraylist 和适配器声明为公共,并在 MyFirebaseMessagingService 中更新此方法

      private void handleNotification(final String message) {
      
      if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
      
      //add message to list
      Fragment.list.add(message);
      Fragment.adapter.notifyDataSetChanged();
          Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
             pushNotification.putExtra("message", message);                                                                                                                                         LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
      
              // play notification sound
              NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
              notificationUtils.playNotificationSound();
      
          } else {
              // If the app is in background, firebase itself handles the notification
          }
      }
      

      【讨论】:

        【解决方案3】:

        您需要在片段中创建一个广播接收器,并且当您的 Firebase 通知一次收到时,您还可以通过以下行调用您的片段接收器

        public String push_receiver = "com.example.fragment.GCMBroadCast";
        Intent intent = new Intent(push_receiver);
                            intent.putExtra("extra", bundle);
                            sendBroadcast(intent);
        

        【讨论】:

        • 注册广播接收器时的意图过滤器是什么? LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mYourBroadcastReceiver, new IntentFilter(?));
        【解决方案4】:
        1. 创建静态方法并从MyFirebaseMessagingService 调用它。 reference1 , reference2
        2. 创建BroadcastReceiver
        3. 使用EventBus

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多