我想将推送通知发送到firebase中的特定用户组。例如,在宠物字段中的应用程序的注册表单中,用户将宠物类型设置为dog.now我想将推送通知发送到有狗的用户。 有人请建议我怎么用firebase做这个?
答案 0 :(得分:1)
首先,您必须在分析中指定哪些宠物包含狗。
Firebase云消息传递(FCM)提供三种不同类型的目标,允许您将推送通知发送到指定的用户组。这三种类型的目标是:用户细分,主题和单个设备。
在用户细分中,您可以选择用于发送推送通知的应用。点击" AND"添加更多过滤规则。
在观众中 - 您可以向宠物狗的用户发送推送通知。
答案 1 :(得分:0)
尝试使用此代码:
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;
}
}
答案 2 :(得分:0)
您通常会使用主题消息传递。使用主题,您的用户可以订阅主题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);
})