【问题标题】:How to send push notification to a specific group of user in firebase如何向firebase中的特定用户组发送推送通知
【发布时间】:2017-05-20 22:42:28
【问题描述】:

我想向 firebase 中的特定用户组发送推送通知。例如,在宠物字段中应用程序的注册表单中,用户将宠物类型设置为狗。现在我想将推送通知发送到养狗的用户。 有人请建议我如何使用firebase做到这一点?

【问题讨论】:

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


    【解决方案1】:

    首先,您必须在分析中指定宠物包含狗的受众。

    这个截图是我的例子

    Firebase 云消息传递 (FCM) 提供三种不同类型的目标,可让您向指定的用户组发送推送通知。三种类型的目标是:用户细分、主题和单个设备。

    这个截图是我的例子

    在用户细分中,您可以选择用于发送推送通知的应用。点击“AND”添加更多过滤规则。

    在观众中 - 您可以向宠物有狗的用户发送推送通知。

    【讨论】:

      【解决方案2】:

      试试这段代码:

      public class NotifyListAdapter extends BaseAdapter implements Constants {
      
              private Activity activity;
              private LayoutInflater inflater;
              private List<Notify> notifyList;
      
              ImageLoader imageLoader = App.getInstance().getImageLoader();
      
              public NotifyListAdapter(Activity activity, List<Notify> notifyList) {
      
                  this.activity = activity;
                  this.notifyList = notifyList;
              }
      
              @Override
              public int getCount() {
      
                  return notifyList.size();
              }
      
              @Override
              public Object getItem(int location) {
      
                  return notifyList.get(location);
              }
      
              @Override
              public long getItemId(int position) {
      
                  return position;
              }
      
              static class ViewHolder {
      
                  public TextView notifyTitle;
                  public TextView notifyTimeAgo;
                  public CircularImageView notifyAuthor;
                  public CircularImageView notifyType;
      
              }
      
              @Override
              public View getView(int position, View convertView, ViewGroup parent) {
      
                  ViewHolder viewHolder = null;
      
                  if (inflater == null) {
      
                      inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                  }
      
                  if (convertView == null) {
      
                      convertView = inflater.inflate(R.layout.notify_list_row, null);
      
                      viewHolder = new ViewHolder();
      
                      viewHolder.notifyAuthor = (CircularImageView) convertView.findViewById(R.id.notifyAuthor);
                      viewHolder.notifyType = (CircularImageView) convertView.findViewById(R.id.notifyType);
                      viewHolder.notifyTitle = (TextView) convertView.findViewById(R.id.notifyTitle);
                      viewHolder.notifyTimeAgo = (TextView) convertView.findViewById(R.id.notifyTimeAgo);
      
                      convertView.setTag(viewHolder);
      
                  } else {
      
                      viewHolder = (ViewHolder) convertView.getTag();
                  }
      
                  if (imageLoader == null) {
      
                      imageLoader = App.getInstance().getImageLoader();
                  }
      
                  viewHolder.notifyTitle.setTag(position);
                  viewHolder.notifyTimeAgo.setTag(position);
                  viewHolder.notifyAuthor.setTag(position);
                  viewHolder.notifyType.setTag(position);
                  viewHolder.notifyAuthor.setTag(R.id.notifyAuthor, viewHolder);
      
                  final Notify notify = notifyList.get(position);
      
                  viewHolder.notifyType.setVisibility(View.GONE);
      
                  if (notify.getType() == NOTIFY_TYPE_LIKE) {
      
                      viewHolder.notifyTitle.setText(activity.getText(R.string.label_gcm_like));
                      viewHolder.notifyType.setImageResource(R.drawable.notify_like);
      
                      viewHolder.notifyAuthor.setImageResource(R.drawable.notify_like);
      
                  } else if (notify.getType() == NOTIFY_TYPE_COMMENT) {
      
                      viewHolder.notifyTitle.setText(activity.getText(R.string.label_gcm_comment));
                      viewHolder.notifyType.setImageResource(R.drawable.notify_comment);
      
                      viewHolder.notifyAuthor.setImageResource(R.drawable.notify_comment);
      
                  } else if (notify.getType() == NOTIFY_TYPE_COMMENT_REPLY) {
      
                      viewHolder.notifyTitle.setText(activity.getText(R.string.label_gcm_comment_reply));
                      viewHolder.notifyType.setImageResource(R.drawable.notify_reply);
      
                      viewHolder.notifyAuthor.setImageResource(R.drawable.notify_reply);
      
                  } else {
      
                      viewHolder.notifyTitle.setText(activity.getText(R.string.label_follow_you));
                      viewHolder.notifyType.setImageResource(R.drawable.notify_follower);
      
                      viewHolder.notifyAuthor.setImageResource(R.drawable.notify_follower);
                  }
      
                  viewHolder.notifyTimeAgo.setText(notify.getTimeAgo());
      
                  return convertView;
              }
          }
      

      【讨论】:

        【解决方案3】:

        您通常会为此使用主题消息传递。使用主题,您的用户将订阅主题 pet_type_dog。在 Android 上,这看起来像这样:

        FirebaseMessaging.getInstance().subscribeToTopic("pet_type_dog");
        

        然后您向所有宠物类型等于狗的用户发送消息。使用 JavaScript Admin SDK,如下所示:

        admin.messaging().sendToTopic("pet_type_dog", {
          notification: {
            title: "Monday message for dog lovers",
            body: "Today's special dog is Dee Dee from SF."
          }
        }).then(function(response) {
            // See the MessagingTopicResponse reference documentation for the
            // contents of response.
            console.log("Successfully sent message:", response);
          })
          .catch(function(error) {
            console.log("Error sending message:", error);
          })
        

        【讨论】:

        • 我要制作一个与血液相关的安卓应用......用户将注册填写他的血型和他的位置。 Firebase数据库中也会有一个请求部分。当用户请求特定位置的特定血型时,应自动向在该位置具有该特定血型的用户发送推送通知。我想要到firebase云功能和推送通知。请建议我该怎么做。
        • 我的回答显示了您需要的方法和大部分代码。请尝试解决方案并告诉您遇到问题的部分。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-08
        • 2017-08-19
        相关资源
        最近更新 更多