如何使用FCM向特定用户发送通知?

Olc*_*mez 23 notifications android firebase google-cloud-messaging firebase-cloud-messaging

我为FCM准备了接收器,可以向所有设备发送通知.

带有此链接的gcm-http.googleapis.com/gcm/send可以发送给已注册的目标用户并将其发布到目标设备,如下面的json:

 {
     "notification": {
                "title": "sample Title",
                "text": "sample text"   },   
        "to" : "[registration id]"
         }
Run Code Online (Sandbox Code Playgroud)

但是,我需要通过电子邮件或姓名等向我选择的目标用户发送通知.例如:

{
     "notification": {
                "title": "sample Title",
                "text": "sample text"   },   
        "to" : "[email or name or sex ...]"
         }
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?我是否需要创建Web服务器或其他东西?

Tim*_*Tim 30

我需要创建一个Web服务器吗?

是.您需要一个可以将姓名/电子邮件映射到注册ID的地方.例如,这些注册ID必须包含在FCM的请求中

{
    'registration_ids': ['qrgqry34562456', '245346236ef'],
    'notification': {
        'body': '',
        'title': ''
    },
    'data': {

    }
}
Run Code Online (Sandbox Code Playgroud)

将推送发送到'qrgqry34562456'和'245346236ef'.

您在呼叫中使用的注册ID是应用程序中此回调中称为"令牌"的ID.

public class MyService extends FirebaseInstanceIdService {
    @Override
    public void onTokenRefresh() {
    }
}
Run Code Online (Sandbox Code Playgroud)

  • registration_id 和注册令牌是一回事吗? (3认同)
  • @AnggaAriWijaya 是的 (2认同)
  • @TheWebGuy 是的。如果您不能告诉他们是谁,您如何告诉 Firebase 向谁发送消息? (2认同)