Shawon Chowdhury
Shawon Chowdhury

Reputation: 1

How to send push notification to a specific group of user in firebase

I want to send push notifications to a specific group of user in firebase.For example, in the sign up form of a app in the pet field a user put pet type as dog.now I want to send push notification to the user who have dog. anybody please suggest me how can I do this with firebase?

Upvotes: 0

Views: 2018

Answers (3)

Jaydeep Kataria
Jaydeep Kataria

Reputation: 867

First you have to specify audience in analytics who's pet contains dog.

enter image description here this screenshot is example of mine

Firebase Cloud Messaging (FCM) offers three different types of targets, which allows you to send push notifications to a specified user group. The three types of targets are: user segment, topic, and single device.

enter image description here this screenshot is example of mine

In User segment, you can choose an app for sending out push notifications. Click "AND" to add more filter rules.

In audience - you can send push notifications to the users who's pet have dog.

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 598688

You'd typically use topic messaging for that. With topics your user would subscribe to the topic pet_type_dog. On Android this would look like this:

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

Then you send a message to all users with pet type equal to dog. With the JavaScript Admin SDK that would look like:

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);
  })

Upvotes: 0

Aoun allah Billel
Aoun allah Billel

Reputation: 47

try with this code :

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;
        }
    }

Upvotes: 0

Related Questions